tags:

views:

49

answers:

3

I have a DataGridView in a form and I want to programatically click its first row. I have found code to select its rows or columns from code.

For eg. datagridview.Columns[0].Selected = true; datagridview.Rows[0].Selected = true;

However this code is not raising the click event on the datagridview. If any one has coded how to click a datagridview from code, please extend your kind help.

Thanks for your help guys!

+4  A: 

Simply call the event handler method e.g.:

datagridviewRowClickedEventHandler(new object(), new eventargs());

If you use the sender or e parameters in the event handler then you will need to work out how to pass in the correct values.

Ben Robinson
+1  A: 

It looks like you have the first half, setting the propers rows Selected value to true. Now you can programatically call the row click handler and it should proceed as if you had clicked it within the GUI.

Robb
+1  A: 

Insert the follwing code into your project where appropriate (Usually on the form which has the datagridview).
Make sure to change the name of the DataGridView from dataGridView1 to the appropriate one on your form.

private void Form1_Load(object sender, EventArgs e)
{
    //call the cell click event with the first cell as the parameters.
    dataGridView1_CellClick(dataGridView1, new DataGridViewCellEventArgs(0, 0));
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    //put your code for handling cell click here
}
Alex Essilfie