After the Windows Forms DataGrid is connected to a database, you can monitor which cell the user clicked.
To detect when the user of the DataGrid selects a different cell
- In the CurrentCellChanged event handler, write code to respond appropriately.
' Visual Basic
Private Sub myDataGrid_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles myDataGrid.CurrentCellChanged
MessageBox.Show("Col is " & myDataGrid.CurrentCell.ColumnNumber _
& ", Row is " & myDataGrid.CurrentCell.RowNumber _
& ", Value is " & myDataGrid.Item(myDataGrid.CurrentCell))
End Sub
// C#
private void myDataGrid_CurrentCellChanged(object sender,
System.EventArgs e)
{
MessageBox.Show ("Col is " + myDataGrid.CurrentCell.ColumnNumber
+ ", Row is " + myDataGrid.CurrentCell.RowNumber
+ ", Value is " + myDataGrid[myDataGrid.CurrentCell] );
}
Visual C# Note Be sure that the necessary code to enable the event handler is present. In this case, it would be similar to the following:
this.myDataGrid.CurrentCellChanged += new
System.EventHandler(this.myDataGrid_CurrentCellChanged);
To determine which part of the DataGrid the user clicked
See Also
DataGrid Control (Windows Forms) | Changing Displayed Data at Run Time in the Windows Forms DataGrid Control