gpt4 book ai didi

c# - 如何从按钮调用 Datagridview 单元格双击事件?

转载 作者:行者123 更新时间:2023-12-02 04:06:57 25 4
gpt4 key购买 nike

我正在尝试从另一个按钮调用 datagridview 单元格事件方法。

DataGridView单元格双击方法

private void ListDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
ListDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
DataGridViewRow row = this.ListDataGridView.Rows[e.RowIndex];

comboBox2.Text = row.Cells[1].Value.ToString();

}
}

这是我调用此方法的按钮

private void button6_Click(object sender, EventArgs e)
{
ListDataGridView_CellDoubleClick(sender, e);
}

我收到错误

Error 3 The type or namespace name 'DataGridViewCellEventHandler' does not exist in the namespace 'System' (are you missing an assembly reference?) C:\VisualC#\Projects\DataGridViewApplication\DataGridViewApplication\List.Designer.cs 340 46 DataGridViewApplication

我做了什么:

我将 EventArgs 更改为 DataGridViewCellEventArgs。

private void button6_Click(object sender, DataGridViewCellEventArgs e)
{
ListDataGridView_CellDoubleClick(sender, e);
}

现在我收到错误:

this.button6.Click += new System.EventHandler(this.button6_Click);

Error 3 No overload for 'button6_Click' matches delegate 'System.EventHandler'

C:\VisualC#\Projects\DataGridViewApplication\DataGridViewApplication\List.Designer.cs 340 35 DataGridViewApplication

现在我将按钮事件处理程序代码更改为此

this.button6.Click += new System.DataGridViewCellEventHandler(this.button6_Click);

仍然收到此错误并卡在此处

Error 3 No overload for 'button6_Click' matches delegate 'System.EventHandler'

在这里找到解决方案: How to call a datagridview event with a click of a button?

private void button6_Click(object sender, EventArgs e)
{
ListDataGridView_CellDoubleClick(null, null);
}

但这对我不起作用,它给我一个错误。

Object Reference not set to an instance of an object.

最佳答案

    private void button6_Click(object sender, EventArgs e)
{

ListDataGridView_CellDoubleClick(this.ListDataGridView, new DataGridViewCellEventArgs(this.ListDataGridView.CurrentCell.ColumnIndex,this.ListDataGridView.CurrentRow.Index));

}

关于c# - 如何从按钮调用 Datagridview 单元格双击事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39015557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com