以编程方式从另一个Button执行单击DataGridView Button Cell

问题描述 投票:1回答:2

我有一个关于.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

先感谢您!

.net vb.net winforms datagridview
2个回答
1
投票

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

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

VB.net

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

要以编程方式单击特定行中的按钮,您可以调用您创建的方法作为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 - 将逻辑放在另一个方法中,并在需要时调用该方法

作为另一个选项,您可以将相关逻辑放在方法中单击单元格按钮,这取决于CellRow对象,并且只将合适的值传递给该方法。然后您可以在任何需要的地方调用该方法。

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 - 通过调用事件处理程序执行Click for DataGridView Button Cell

要以编程方式单击特定行中的按钮,您可以调用您创建的方法作为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 - 将逻辑放在另一个方法中,并在需要时调用该方法

作为另一个选项,您可以将相关逻辑放在方法中单击单元格按钮,这取决于CellRow对象,并且只将合适的值传递给该方法。然后您可以在任何需要的地方调用该方法。

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

1
投票

如果要以编程方式生成单击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);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.