Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid to New Form Text boxes

Status
Not open for further replies.

Igawa29

MIS
Jan 28, 2010
99
US
So I have been trying to figure this issue out and am not sure where to begin.

I have a datagrid which I have coded where the user is able to select 1 row only. I wanted to create an edit button that would capture the data within that row and then populate another form's text boxes that the user would be able to edit. Then they would click another button that would update the table.

So basically I want the datagrid only to look at the data and then the text box form to edit and change data.

Any tips?
 
Well, there's quite a bit involved in something like this, so I'm going to give a broad overview and you can ask questions as needed.

First, create the edit form with the textboxes in it. Then, at the top of the form's code in the Declarations section put this code:

Code:
    Dim drDataToEdit As DataRow

    Public Property DataToEdit() As DataRow
        Get
            Return drDataToEdit
        End Get
        Set(ByRef value As DataRow) '<- make sure this parameter is ByRef, not ByVal
            drDataToEdit = value
        End Set
    End Property


    Dim bCancel As Boolean

    Public Property CancelEdit() As Boolean
        Get
            Return bCancel
        End Get
        Set(ByVal value As Boolean)
            bCancel = value
        End Set
    End Property

This is how you will pass the DataRow to be edited to the edit form.

Now in the form with the DataGrid, put this in the Declarations section:

Dim cm As CurrencyManager

Next, where you bind data to the DataGrid, put this code:

cm = DataGrid1.BindingContext(DataTable)

Note: use the name of your DataGrid, and the name of the DataTable bound to the grid.

Next, in the DataGrid's MouseUp event handler, put this code:

Code:
    Private Sub DataGrid1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseUp
        If DataGrid1.CurrentRowIndex <> -1 Then
            DataGrid1.Select(dg.CurrentRowIndex)
        End If
    End Sub
Again, use the name of your DataGrid.

Next, put an edit button and put this in the code of that button:

Code:
    Dim dr As DataRow

    dr = cm.Current

    Dim fEdit as New frmEdit

    fEdit.DataToEdit = dr

    fEdit.ShowDialog()

    If fEdit.CancelEdit Then
        DataTable.RejectChanges()
    Else
        If DataTable.HasChanges() Then
            'code to save changes
        EndIf
    EndIf

Now put 2 more buttons on the edit form. Name them "btnSave" and "btnCancel"

In the code for btnSave put this:

Me.Cancel = False
Me.Close

in the code for btnCancel put this:

Me.Cancel = True
Me.Close

That's pretty much it.





I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thank you, this is a good kick off point for me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top