You can use either of the following options:
- Calling the event handler of
CellContentClick like a normal method by creating an instance of DataGridViewCellEventArgs and pass it to the event handler method.
- Or put the whole logic inside a method and call that method whenever you need, from
CellContentClick of the DataGridView or Click of the button.
VB.NET
Example 1 - Perform Click for DataGridView Button Cell by calling the event handler
To programmatically click on button in specific row, you can call the method that you created as event handler of CellContentClick event, using suitable DataGridViewCellEventArgs as e and your DataGridView as sender:
Private Sub AnotherButton_Click(sender As Object, e As EventArgs) _
Handles AnotherButton.Click
' zero based ColumnIndex of your button column= 3 (for example)
' zero based RowIndex that you want to click on its button column = 2 (for example)
Dim arg = New DataGridViewCellEventArgs(3, 2)
DataGridView1_CellContentClick(DataGridView1, arg)
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, _
e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
MessageBox.Show(e.RowIndex.ToString())
End Sub
Example 2 - Putting the logic in another method and call the method when you need
As another option you can put the logic related to click on a cell button in a method, dependent from Cell and Row objects and only pass suitable values to that method. Then you can call the method wherever you need.
Private Sub DoSomething(rowIndex as Integer, columnIndex as Integer)
MessageBox.Show(rowIndex.ToString())
End Sub
Private Sub AnotherButton_Click(sender As Object, e As EventArgs) _
Handles AnotherButton.Click
' zero based ColumnIndex of your button column= 3 (for example)
' zero based RowIndex that you want to click on its button column = 2 (for example)
DoSomething(2, 3)
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, _
e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
DoSomething(e.RowIndex, e.ColumnIndex)
End Sub
C#
Example 1 - Perform Click for DataGridView Button Cell by calling the event handler
To programmatically click on button in specific row, you can call the method that you created as event handler of CellContentClick event, using suitable DataGridViewCellEventArgs as e and your DataGridView as sender:
private void anotherButton_Click(object sender, EventArgs e)
{
' zero based ColumnIndex of your button column= 3 (for example)
' zero based RowIndex that you want to click on its button column = 2 (for example)
var arg = new DataGridViewCellEventArgs(3, 2);
dataGridView1_CellContentClick(dataGridView1, arg);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show(e.RowIndex.ToString());
}
Example 2 - Putting the logic in another method and call the method when you need
As another option you can put the logic related to click on a cell button in a method, dependent from Cell and Row objects and only pass suitable values to that method. Then you can call the method wherever you need.
private void DoSomething(int rowIndex, int columnIndex)
{
MessageBox.Show(rowIndex.ToString());
}
private void anotherButton_Click(object sender, EventArgs e)
{
' zero based ColumnIndex of your button column= 3 (for example)
' zero based RowIndex that you want to click on its button column = 2 (for example)
DoSomething(2, 3);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DoSomething(e.RowIndex, e.ColumnIndex);
}