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!