selectedindex or currentrowindex change on datagrid?

doraemon

Freshman
Joined
Oct 3, 2003
Messages
36
I wonder if there is any method/event to tell if the user goes from one row to another in the datagrid? There are rowchanged and rowchanging events; however, they won't fire unless there are things changed in the row. What I need to do is when the user clicks on any cell on another row, the child's datagrid will be refreshed. However, I could not get it working. The click event seems to only fire if the user clicks on "row heading" (the little box on the left). When they click on the cell, the click event did not seem to fire. So, I'm kind of thinking about putting codes in a selectedindexchange or currentrowindex change event but I do not seem to see such an option. Does anyone have any good suggestion?
 
Try using CurrentCellChanged event. Create a private int variable at form level, and initialize it with -1. Inside that event, check if the datagrid.CurrentrowIndex property is different from the variable. If it's true, then user went from one cell to another. At the end of the event, update the variable with the datagrid.CurrentrowIndex property.
 
I've read somewhere here about the MouseUp event you can also use (this fires after a mouseclick). Use the HitTest to recover the row and column (look in the VS-help).

Example coding for highlighting a full datagridrow:

Private Sub onDataGridMouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseUp
Dim pt = New Point(e.X, e.Y)
Dim hit As DataGrid.HitTestInfo = DataGrid1.HitTest(pt)

If hit.Type = Windows.Forms.DataGrid.HitTestType.Cell Then
DataGrid1.CurrentCell = New DataGridCell(hit.Row, hit.Column)
DataGrid1.Select(hit.Row)
End If
End Sub
 
Last edited:
Back
Top