Select DatagridView first column Rows from Button click.

Abdoul Rahim

Member
Joined
Oct 16, 2017
Messages
15
Programming Experience
Beginner
Hi guys. I have a button and a datagridView fill with data in my form. So , is it possible to automatically select the datagridview first row columns (Current Row) when the button is clicked?
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,741
Location
Sydney, Australia
Programming Experience
10+
It's not clear what you mean. Remember that "selected" and "current' mean different things in a DataGridView. Do you mean that you want to select all the cells in first column? Do you want to make the first cell in the current row the current cell? Do you want something else?
 

Abdoul Rahim

Member
Joined
Oct 16, 2017
Messages
15
Programming Experience
Beginner
Sorry, i want to make the first cell in the current row Selected when i click on the button.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,741
Location
Sydney, Australia
Programming Experience
10+
If you actually mean selected then you get a reference to that cell and set its Selected property to True. To get that reference, you get the Cells property of the CurrentRow and get the item at index 0.
 

Abdoul Rahim

Member
Joined
Oct 16, 2017
Messages
15
Programming Experience
Beginner
Thank very much. ..
But please can i have any code line which can enable me to better understand how toi set cell crurent row property to index 0..
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
627
Location
Norway
Programming Experience
10+
get the Cells property of the CurrentRow and get the item at index 0
var cell = theDataGridView.CurrentRow.Cells[0];

get a reference to that cell and set its Selected property to True
cell.Selected = true;

or shorter:
theDataGridView.CurrentRow.Cells[0].Selected = true;
 
Top Bottom