.net 以编程方式从另一个按钮单击DataGridView按钮单元格
我有一个关于.NET中的.net 以编程方式从另一个按钮单击DataGridView按钮单元格,.net,vb.net,winforms,datagridview,.net,Vb.net,Winforms,Datagridview,我有一个关于.NET中的DataGridView控件的问题 我从工具箱中插入了一个DataGridView,并将其与我在access中设置的数据库连接起来。然后,我从DataGridViewtasks面板的编辑列中添加了一个带有按钮的列 DataGridView按钮的点击事件工作正常 我想在单击DataGridView之外的另一个按钮时,以编程方式单击DataGridView按钮。我该怎么做 DataGridView的代码为: Private Sub dgvAnimSel_CellContent
DataGridView
控件的问题
我从工具箱中插入了一个DataGridView
,并将其与我在access中设置的数据库连接起来。然后,我从DataGridView
tasks面板的编辑列中添加了一个带有按钮的列
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
提前谢谢你 您可以使用以下任一选项:
- 通过创建
的实例并将其传递给事件处理程序方法,调用DataGridViewCellEventArgs
CellContentClick的事件处理程序,就像普通方法一样
- 或者将整个逻辑放在一个方法中,并在需要时调用该方法,从
cellcontent单击
DataGridView的
,或单击按钮的
单击
e
和您的DataGridView
assender
调用创建为CellContentClick
事件的事件处理程序的方法:
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
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-将逻辑放入另一个方法中,并在需要时调用该方法
作为另一个选项,您可以根据单元格
和行
对象,在方法中放置与单击单元格按钮相关的逻辑,并仅将合适的值传递给该方法。然后,您可以在任何需要的地方调用该方法
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
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);
}
C#
示例1-通过调用事件处理程序来执行单击DataGrdiView按钮单元格
要以编程方式单击特定行中的按钮,您可以使用合适的ase
和您的DataGridView
assender
调用创建为CellContentClick
事件的事件处理程序的方法:
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
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-将逻辑放入另一个方法中,并在需要时调用该方法
作为另一个选项,您可以根据单元格
和行
对象,在方法中放置与单击单元格按钮相关的逻辑,并仅将合适的值传递给该方法。然后,您可以在任何需要的地方调用该方法
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
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);
}
您可以使用以下任一选项:
- 通过创建
的实例并将其传递给事件处理程序方法,调用DataGridViewCellEventArgs
CellContentClick的事件处理程序,就像普通方法一样
- 或者将整个逻辑放在一个方法中,并在需要时调用该方法,从
cellcontent单击
DataGridView的
,或单击按钮的
单击
e
和您的DataGridView
assender
调用创建为CellContentClick
事件的事件处理程序的方法:
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
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-将逻辑放入另一个方法中,并在需要时调用该方法
作为另一个选项,您可以根据单元格
和行
对象,在方法中放置与单击单元格按钮相关的逻辑,并仅将合适的值传递给该方法。然后,您可以在任何需要的地方调用该方法
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
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);
}
C#
示例1-通过调用事件处理程序来执行单击DataGrdiView按钮单元格
要以编程方式单击特定行中的按钮,您可以使用合适的ase
和您的DataGridView
assender
调用创建为CellContentClick
事件的事件处理程序的方法:
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
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-将逻辑放入另一个方法中,并在需要时调用该方法
作为另一个选项,您可以根据单元格
和行
对象,在方法中放置与单击单元格按钮相关的逻辑,并仅将合适的值传递给该方法。然后,您可以在任何需要的地方调用该方法
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
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);
}
若要以编程方式生成,请单击
DataGridViewButtonCell
实例,可以使用属性和调用方法
类似这样的内容(对于C#,我很抱歉,我相信您可以将其翻译为VB):
测试:
若要以编程方式生成,请单击
DataGridViewButtonCell
实例,可以使用属性和调用方法
类似这样的内容(对于C#,我很抱歉,我相信您可以将其翻译为VB):
测试:
我想以编程方式更改DataGridView按钮的状态更改为什么状态?您指的是什么样的更改?对于更改状态,我指的是单击DataGridView按钮(对不起,我的错误!)我想以编程方式更改DataGridView按钮的状态更改为什么状态?您指的是什么样的更改?对于更改状态,我指的是单击DataGridView按钮(对不起,我的错误!)谢谢你们的帮助!Ivan先生我试过你的建议但没用。。。雷扎先生,你的方法可行,但当我运行应用程序时,它会显示一个DataGridView默认错误对话框,显示System。异常:Selected不是Int32的有效值。。。这是什么意思?再次非常感谢!要独立于您的程序测试我的答案,请像我一样安排一个简单的测试
MessageBox.Show(e.RowIndex.ToString())
我忘记使用.ToString()
,然后如果它工作(将正常工作),则表示解决方案正常,您应该将其应用于y