How to catch the event in DataGrid?!


New member
Aug 10, 2004
Programming Experience
I have created a table using datagrid in I want to catch the event when make any changes in one of the cells in the data grid. I have tried almost all the properties for the datagrid, but i couldn't find a solution!!.

cell change event

There is no cell change event that i'm aware of. Based on your question I'm going to assume a couple of things. First, I'll assume you're connected to a backend database - be it, MS Access, SS2K, Oracle or something else. And secondly, I'm going to assume you want to perform some data validation on the contents of the cell. If this is the case then the best place to perform data validation is at the database level. Therefore, create a constraint/trigger on the field in question to check for your business rules. If the rules are not met, raise an event w/in the trigger to issue an error. Through your dataadapter in your program you can trap the error in the update/insert method and deal with it there.

Good luck.
You can use the OnColumnChanging or OnRowChanging events.

First you need to add the following to the Sub New() procedure.
AddHandler Me.MydataSet.Tables(0).RowChanging, New DataRowChangeEventHandler(AddressOf OnRowChanging)

AddHandler Me.MyDataSet.Tables(0).ColumnChanging, New DataColumnChangeEventHandler(AddressOf OnColumnChanging)

Then add these functions to your form:

PrivateSharedSub OnRowChanging(ByVal sender AsObject, ByVal args As DataRowChangeEventArgs)
If args.Action <> DataRowAction.Nothing Then
Dim actionStr As String
If args.Action = DataRowAction.Change Then
actionStr = System.Enum.GetName(args.Action.GetType(), args.Action)
MsgBox(" RowChanging: Action = " & actionStr & ", StepNum = " & args.Row("StepNum").ToString())
End If
End If

PrivateSharedSub OnColumnChanging(ByVal sender AsObject, ByVal args As DataColumnChangeEventArgs)

'Console.Write("ColumnChanging: ")
MsgBox(args.Column.ColumnName & " equals '" & args.Row (args.Column).ToString() & _
"', changing to '" & args.ProposedValue.ToString() & "'" & vbCrLf)

Of course you need to change the OnChanging functions to do what you want to do.