I have a question regarding DataGridView
control in .NET.
I inserted a DataGridView
from the toolbox and I connected it with a database that I
You can use either of the following options:
CellContentClick
like a normal method by creating an instance of DataGridViewCellEventArgs
and pass it to the event handler method.CellContentClick
of the DataGridView
or Click
of the button.Example 1 - Perform Click for DataGrdiView 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
Example 1 - Perform Click for DataGrdiView 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);
aataGridView1_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);
}
If you want to generate programmatically click on DataGridViewButtonCell
instance, you can use DataGridViewCell.AccessibilityObject property and call DoDefaultAction method.
Something like this (sorry for C#, I'm sure you can translate it to VB):
DataGridViewButtonCell otherCell = ...;
otherCell.AccessibilityObject.DoDefaultAction();
Test:
using System;
using System.Linq;
using System.Windows.Forms;
namespace Samples
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form = new Form();
var grid = new DataGridView { Dock = DockStyle.Fill, Parent = form, AutoGenerateColumns = false };
var col0 = new DataGridViewTextBoxColumn { Name = "Col0", HeaderText = "Col0", DataPropertyName = "Col0" };
var col1 = new DataGridViewButtonColumn { Name = "Col1", HeaderText = "Col1", DataPropertyName = "Col1" };
grid.Columns.AddRange(new DataGridViewColumn[] { col0, col1 });
grid.CellContentClick += (sender, e) =>
{
MessageBox.Show("Clicked Cell[" + e.RowIndex + "," + e.ColumnIndex + "]");
};
grid.DataSource = Enumerable.Range(0, 10).Select(n => new { Col0 = "Cell[" + n + ",0]", Col1 = "Cell[" + n + ",1]" }).ToList();
var button = new Button { Dock = DockStyle.Bottom, Parent = form, Text = "Click" };
button.Click += (sender, e) =>
{
var cell = grid.CurrentRow.Cells[col1.Index];
cell.AccessibilityObject.DoDefaultAction();
};
Application.Run(form);
}
}
}