8

Possible Duplicate:
Selecting a row in Datagridview Programatically?

I am developing a new desktop application in C# using Windows Forms. In one of my form i put the DataGridView Control and i am populating this dataGridViewControl Dynamically using my custom functions.

Now after populating the above control, is there any way to programatically select the first row of that DataGridViewview. Note: the Selection mode property of this DataGridView is set to "Full row Select"

Community
  • 1
  • 1
Jame
  • 21,150
  • 37
  • 80
  • 107

2 Answers2

30

Try:

dataGridView1.Rows[0].Selected = true;
Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
  • 1
    if grid is empty throws an error. It's bettere make a check before. if (dataGridView1.Rows.Count > 0) {...} – Evilripper Apr 26 '21 at 07:42
5

To select one row in winform DataGridView TRY THIS:

dataGridView1.MultiSelect = false;
dataGridView1.MultiSelect = true;
dataGridView1.Rows[RowIndex].Selected = true;

It works ..

Tarek Sultan
  • 51
  • 1
  • 3
  • 1
    One might need to scroll to selected row to be visible. dataGridView1.FirstDisplayedScrollingRowIndex = RowIndex; – Bronek Feb 08 '13 at 23:24