我对.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
提前谢谢!
答案 0 :(得分:1)
您可以使用以下任一选项:
CellContentClick
的实例并将其传递给事件处理程序方法,像正常方法一样调用DataGridViewCellEventArgs
的事件处理程序。CellContentClick
或DataGridView
按钮{/ 1}}调用该方法。示例1 - 通过调用事件处理程序执行Click for DataGrdiView Button Cell
要以编程方式单击特定行中的按钮,您可以使用Click
CellContentClick
作为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
:
Cell
示例2 - 将逻辑放在另一个方法中,并在需要时调用该方法
作为另一个选项,您可以将相关逻辑放在方法中单击单元格按钮,这取决于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
对象,并且只将合适的值传递给该方法。然后,您可以在任何需要的地方调用该方法。
CellContentClick
示例1 - 通过调用事件处理程序执行Click for DataGrdiView Button Cell
要以编程方式单击特定行中的按钮,您可以使用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());
}
来调用您创建的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);
}
示例2 - 将逻辑放在另一个方法中,并在需要时调用该方法
作为另一个选项,您可以将相关逻辑放在方法中单击单元格按钮,这取决于$ if [[ "test" =~ \w+ ]]; then echo "yes"; else echo "no"; fi
no
$ if [[ "t" =~ \w+ ]]; then echo "yes"; else echo "no"; fi
no
$ if [[ "w" =~ \w+ ]]; then echo "yes"; else echo "no"; fi
yes
$ if [[ "wwwww" =~ \w+ ]]; then echo "yes"; else echo "no"; fi
yes
和$ if [[ "test" =~ "\w+" ]]; then echo "yes"; else echo "no"; fi
no
对象,并且只将合适的值传递给该方法。然后,您可以在任何需要的地方调用该方法。
.sh
答案 1 :(得分: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);
}
}
}