以编程方式从另一个按钮上单击DataGridView按钮单元 [英] Programmatically perform click on a DataGridView Button Cell from another Button

查看:47
本文介绍了以编程方式从另一个按钮上单击DataGridView按钮单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a question regarding DataGridView control in .NET.

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

I inserted a DataGridView from 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 DataGridView tasks panel.

DataGridView 按钮可以正常工作!

The click events of the DataGridView buttons work without a problem!

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

I want to perform a click on DataGridView button programmatically when I click another button outside of the DataGridView. How should I do this?

DataGridView的代码是:

The code of the DataGridView is:

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

谢谢!

推荐答案

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

You can use either of the following options:


  • 像常规方法一样调用 CellContentClick 的事件处理程序通过创建 DataGridViewCellEventArgs 的实例并将其传递给事件处理程序方法。

  • 或将整个逻辑放入方法中并调用该方法方法,无论何时需要,都可以从 DataGridView Click CellContentClick

  • 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.

示例1-对DataGrdiView B执行点击通过调用事件处理程序来创建utton Cell

要以编程方式单击特定行中的按钮,您可以调用作为<$ c的事件处理程序创建的方法$ c> CellContentClick 事件,使用适当的 DataGridViewCellEventArgs 作为 e 和您的 DataGridView 作为发件人

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

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

您可以将与单击单元格按钮相关的逻辑放在方法中,该方法取决于 Cell Row 对象,并且仅将适当的值传递给该方法。然后您可以在任何需要的地方调用该方法。

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#



示例1 -通过调用事件处理程序执行对DataGrdiView按钮单元的单击

要以编程方式单击特定行中的按钮,可以调用y使用适当的CellContentClick 事件的事件处理程序.datagridviewcelleventargs(v = vs.110).aspx rel = nofollow noreferrer> DataGridViewCellEventArgs 作为 e 和您的 DataGridView 作为发件人

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

示例2-将逻辑放入另一个方法并调用该方法

作为另一种选择,您可以将与单击单元格按钮相关的逻辑放在方法中,该方法取决于 Cell Row 对象,仅将适当的值传递给该方法。然后您可以在需要的地方调用该方法。

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

这篇关于以编程方式从另一个按钮上单击DataGridView按钮单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆