Я пытаюсь вызвать метод события ячейки datagridview с другой кнопки.Как вызвать событие двойного щелчка ячейки Datagridview с помощью кнопки?
DataGridView Cell Double Click Метод
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);
Тем не менее эта ошибка и stucked здесь
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.
В качестве другого варианта вместо того, чтобы назвать обработчик события 'CellDoubleClick', вы можете поместить логику в методе, как' DoSomething' и назвать его как в 'случае CellDoubleClick' и' Click' события вашей кнопки. Например, взгляните на [этот пост] (http://stackoverflow.com/a/38611739/3110834). –