I'm trying to call datagridview cell event method from an another button.
DataGridView Cell Double Click Method
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();
}
}
This is Button where I'm calling this method
private void button6_Click(object sender, EventArgs e)
{
ListDataGridView_CellDoubleClick(sender, e);
}
Error I'm receiving
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
What I did:
I changed EventArgs to DataGridViewCellEventArgs.
private void button6_Click(object sender, DataGridViewCellEventArgs e)
{
ListDataGridView_CellDoubleClick(sender, e);
}
Now I'm receiving Error:
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
Now I changed button event handler code to this
this.button6.Click += new System.DataGridViewCellEventHandler(this.button6_Click);
Still Receiving this error and stucked here
Error 3 No overload for 'button6_Click' matches delegate 'System.EventHandler'
Found a solution here: How to call a datagridview event with a click of a button?
private void button6_Click(object sender, EventArgs e)
{
ListDataGridView_CellDoubleClick(null, null);
}
but this is not working for me, it gives me an error.
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));
}
I would expect you have this at the top of you Form.cs file:
using System.Windows.Forms;
Eventhandlers are strongly typed, so both event and handler need to have matching types.
The Click
eventhandler requires a method signature of void(object sender, EventArgs e)
so your initial code was the right approach:
private void button6_Click(object sender, EventArgs e)
{
// create and set values for the event argument.
// it can't be EventArgs, so just instantiate the right type
// the constructor needs a row and column
var datagridviewArgs = new DataGridViewCellEventArgs(42,13);
ListDataGridView_CellDoubleClick(sender, datagridviewArgs);
}
but as you can see you need to provide the correct type for the second parameter, the DataGridViewCellEventArgs , on the call to ListDataGridView_CellDoubleClick
.
Use this...
private void btnChoose_Click(object sender, EventArgs e)
{
MouseEventArgs b = new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 2,
MousePosition.X, MousePosition.Y, 0);
DataGridViewCellMouseEventArgs a = new DataGridViewCellMouseEventArgs(0, 0,
MousePosition.X, MousePosition.Y, b);
dataGridView1_CellMouseDoubleClick(sender, a);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.