【问题标题】:Programmatically perform click on a DataGridView Button Cell from another Button以编程方式从另一个按钮执行单击 DataGridView 按钮单元格
【发布时间】:2016-01-10 18:29:12
【问题描述】:

我对 .NET 中的 DataGridView 控件有疑问。

我从工具箱中插入了一个DataGridView,并将它与我在访问中设置的数据库连接起来。然后我从DataGridView 任务面板的编辑列中添加了一个带有按钮的列。

DataGridView 按钮的点击事件没有问题!

当我单击DataGridView 之外的另一个按钮时,我想以编程方式单击DataGridView 按钮。我该怎么做?

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

提前谢谢你!

【问题讨论】:

  • 我想以编程方式更改 DataGridView 按钮的状态 更改为什么状态?你的意思是什么变化?
  • 通过更改状态,我的意思是单击 DataGridView 按钮,对不起我的错误! :)

标签: .net vb.net winforms datagridview


【解决方案1】:

您可以使用以下任一选项:

  • 通过创建DataGridViewCellEventArgs 的实例并将其传递给事件处理方法,像普通方法一样调用CellContentClick 的事件处理程序。
  • 或者将整个逻辑放在一个方法中,并在需要时从按钮的DataGridView 的CellContentClick 或Click 调用该方法。

VB.NET

示例 1 - 通过调用事件处理程序对 DataGrdiView Button Cell 执行 Click

要以编程方式单击特定行中的按钮,您可以调用您创建为CellContentClick 事件的事件处理程序的方法,使用合适的DataGridViewCellEventArgs 作为e 和您的DataGridView 作为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

示例 2 - 将逻辑放在另一个方法中,并在需要时调用该方法

作为另一种选择,您可以将与单击单元格按钮相关的逻辑放入方法中,依赖于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#

示例 1 - 通过调用事件处理程序对 DataGrdiView Button Cell 执行 Click

要以编程方式单击特定行中的按钮,您可以调用您创建为CellContentClick 事件的事件处理程序的方法,使用合适的DataGridViewCellEventArgs 作为e 和您的DataGridView 作为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());
}

示例 2 - 将逻辑放在另一个方法中,并在需要时调用该方法

作为另一种选择,您可以将与单击单元格按钮相关的逻辑放入方法中,依赖于 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);
}

【讨论】:

  • 感谢大家的帮助!伊万先生,我尝试了您的建议,但无法正常工作... Reza 先生,您的方法有效,但是当我运行该应用程序时,它会显示带有 System.Exception 的 DataGridView 默认错误对话框:所选值不是 Int32 的有效值。 .. 这是什么意思?再次非常感谢您!
  • 要独立于您的程序来测试我的答案,请像我一样安排一个简单的测试。 MessageBox.Show(e.RowIndex.ToString()) 我忘了使用 .ToString() 然后如果它工作(这将正常工作)这意味着解决方案是好的,你应该将它应用到你的程序中。
  • @Kostas 我没有检查 Ivan 的答案,但我认为它应该很有用。此外,如果您想提出问题或需要更多帮助,请在 Ivan 的回答下方发表评论。
【解决方案2】:

如果要以编程方式生成单击DataGridViewButtonCell 实例,可以使用DataGridViewCell.AccessibilityObject 属性并调用DoDefaultAction 方法。

类似这样的东西(对不起C#,我相信你可以把它翻译成VB):

DataGridViewButtonCell otherCell = ...;
otherCell.AccessibilityObject.DoDefaultAction();

测试:

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);
        }
    }
}

【讨论】:

  • @Kostas 它也可以正常工作。不要忘记添加对Accessibility 程序集的引用。
猜你喜欢
  • 2014-08-08
  • 2013-10-09
  • 2017-06-17
  • 2013-05-23
  • 1970-01-01
  • 1970-01-01
  • 2015-09-25
相关资源
最近更新 更多