vb.net 以编程方式从另一个按钮单击 DataGridView 按钮单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34709204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically perform click on a DataGridView Button Cell from another Button
提问by Kostas
I have a question regarding DataGridViewcontrol in .NET.
我有一个关于DataGridView.NET 控制的问题。
I inserted a DataGridViewfrom the toolbox and I connected it with a database that I setup in access. Then I added a column with buttons from the edit columns of the DataGridViewtasks panel.
我DataGridView从工具箱中插入了一个,并将其与我在 access 中设置的数据库连接。然后我从DataGridView任务面板的编辑列中添加了一个带有按钮的列。
The click events of the DataGridViewbuttons work without a problem!
DataGridView按钮的点击事件没有问题!
I want to perform a click on DataGridViewbutton programmatically when I click another button outside of the DataGridView. How should I do this?
我想执行上点击DataGridView编程按钮,当我点击的另一个按钮之外DataGridView。我该怎么做?
The code of the DataGridView is:
DataGridView 的代码是:
Private Sub dgvAnimSel_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) _
Handles dgvAnimSel.CellContentClick
Dim V As String = dgvAnimSel.Rows(e.RowIndex).Cells(0).Value
If e.ColumnIndex = 3 Then
If V = 1 Then
If A1 = 1 Then
'this is the uncheck state
Me.dgvAnimSel.CurrentCell.Style.BackColor = Color.White
Me.dgvAnimSel.CurrentCell.Style.ForeColor = Color.Black
Me.dgvAnimSel.CurrentCell.Value = "Select"
ItemTextNew = ItemTextOr + "1"
ItemName = ListView1.FindItemWithText(ItemTextNew, False, 0, True)
ListView1.Items.Remove(ItemName)
A1 = 0
Else
'this is the check state
Me.dgvAnimSel.CurrentCell.Style.BackColor = Color.Green
Me.dgvAnimSel.CurrentCell.Style.ForeColor = Color.White
Me.dgvAnimSel.CurrentCell.Value = "Selected"
a = ListView1.Items.Add(" " + "Animation 1 ", 0)
A1 = 1
End If
End If
End Sub
Thank you in advance!
先感谢您!
采纳答案by Reza Aghaei
You can use either of the following options:
您可以使用以下任一选项:
- Calling the event handler of
CellContentClicklike a normal method by creating an instance ofDataGridViewCellEventArgsand pass it to the event handler method. - Or put the whole logic inside a method and call that method whenever you need, from
CellContentClickof theDataGridVieworClickof the button.
CellContentClick通过创建 的实例DataGridViewCellEventArgs并将其传递给事件处理程序方法,像普通方法一样调用事件处理程序。- 或者将整个逻辑放在一个方法中,并在需要时从按钮
CellContentClick的DataGridView或调用该方法Click。
VB.NET
网络
Example 1 - Perform Click for DataGrdiView Button Cell by calling the event handler
示例 1 - 通过调用事件处理程序为 DataGrdiView 按钮单元执行单击
To programmatically click on button in specific row, you can call the method that you created as event handler of CellContentClickevent, using suitable DataGridViewCellEventArgsas eand your DataGridViewas sender:
要以编程方式单击特定行中的按钮,您可以调用您创建的方法作为事件的事件处理程序CellContentClick,使用合适的DataGridViewCellEventArgsase和您的DataGridViewas 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
示例 2 - 将逻辑放在另一个方法中并在需要时调用该方法
As another option you can put the logic related to click on a cell button in a method, dependent from Celland Rowobjects and only pass suitable values to that method. Then you can call the method wherever you need.
作为另一种选择,您可以将与单击单元格按钮相关的逻辑放在方法中,依赖于Cell和Row对象,并且只将合适的值传递给该方法。然后您可以在任何需要的地方调用该方法。
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#
C#
Example 1 - Perform Click for DataGrdiView Button Cell by calling the event handler
示例 1 - 通过调用事件处理程序为 DataGrdiView 按钮单元执行单击
To programmatically click on button in specific row, you can call the method that you created as event handler of CellContentClickevent, using suitable DataGridViewCellEventArgsas eand your DataGridViewas sender:
要以编程方式单击特定行中的按钮,您可以调用您创建的方法作为事件的事件处理程序CellContentClick,使用合适的DataGridViewCellEventArgsase和您的DataGridViewas 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
示例 2 - 将逻辑放在另一个方法中并在需要时调用该方法
As another option you can put the logic related to click on a cell button in a method, dependent from Celland Rowobjects and only pass suitable values to that method. Then you can call the method wherever you need.
作为另一种选择,您可以将与单击单元格按钮相关的逻辑放在方法中,依赖于Cell和Row对象,并且只将合适的值传递给该方法。然后您可以在任何需要的地方调用该方法。
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);
}
回答by Ivan Stoev
If you want to generate programmatically click on DataGridViewButtonCellinstance, you can use DataGridViewCell.AccessibilityObjectproperty and call DoDefaultActionmethod.
如果要以编程方式生成单击DataGridViewButtonCell实例,可以使用DataGridViewCell.AccessibilityObject属性并调用DoDefaultAction方法。
Something like this (sorry for C#, I'm sure you can translate it to VB):
像这样的东西(对不起 C#,我相信你可以把它翻译成 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);
}
}
}

