Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting rows programmatically in DataGridView

Tags:

I want to select row of previously selected rows after some event my code is as below.

int currentRow = dgvIcbSubsInfo.CurrentCell.RowIndex; //code to execute dgvIcbSubsInfo.Rows[currentRow].Selected = true; 

after executing the code the preview will be as below. but i need to get the symbol > in id = 1272741 (blue selection) and not in 1272737

enter image description here

like image 794
manoj Avatar asked Jan 29 '13 06:01

manoj


People also ask

How to select a specific row in DataGridView c#?

To get the selected rows in a DataGridView controlUse the SelectedRows property. To enable users to select rows, you must set the SelectionMode property to FullRowSelect or RowHeaderSelect.

How to select row in DataGridView vb net?

Answers. "Rows" is a property of the DataGridView that returns all the rows as a collection. For a particular Row, you can set the . Selected property to True (or False) to select (or unselect) that Row.

How can we get selected row data from GridView in ASP NET?

When a row is selected in a GridView control, use the SelectedRow property to retrieve the GridViewRow object that represents that row. This is the same as retrieving the GridViewRow object at the index specified by the SelectedIndex property from the Rows collection.


1 Answers

Probably you might have taken a look at the DataGridView.CurrentRow Property, which is a read-only property:

Gets the row containing the current cell.

But in the remarks section, there is written:

To change the current row, you must set the CurrentCell property to a cell in the desired row.

Also, from the DataGridView.CurrentCell Property, we find out that:

When you change the value of this property, the SelectionChanged event occurs before the CurrentCellChanged event. Any SelectionChanged event handler accessing the CurrentCell property at this time will get its previous value.

So, there is no need that you actually select the currentRow becasue it will be selected when you set the CurrentCell value (unless you have some code to be executed inside the current scope between the SelectionChanged and CurrentCellChanged events). Try this:

//dgvIcbSubsInfo.Rows[currentRow].Selected = true; dgvIcbSubsInfo.CurrentCell = dgvIcbSubsInfo.Rows[currentRow].Cells[0]; 
like image 117
Alex Filipovici Avatar answered Oct 23 '22 08:10

Alex Filipovici