how to change the selected row in datagridview from code behind

4.9k Views Asked by At

Actually im not sure if its called selected index, selected row or not. if not, can someone tell me whats the name of this pointer and how am i able to change it in datagridview?

enter image description here

2

There are 2 best solutions below

2
Jake On

It's SelectedRows. Link to MSDN

To change it in the code behind, you'd need to retrieve the index of the row you want to select and change its Selected property to true

So as an example, if your DataGridView is called myDataGridView has 25 rows, you wanted to select the 15th row programatically. You'd do:

myDataGridView.Rows[14].Selected = true;

Hope that helps.

0
Emanuel Romano On

To change the selected row index you should use the CurrentCell method, then give the row number you're trying to set the focus on:

YourDataGridView.CurrentCell = YourDataGridView.Rows[RowNumber].Cells[0];