25

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

Community
  • 1
  • 1
manoj
  • 5,235
  • 7
  • 24
  • 45

4 Answers4

56

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];
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
  • 1
    That's a really nice digging in to the code and comments, as well as a thorough explanation of how you got your answer. Thank you Alex! – Jody Sowald Oct 02 '20 at 14:14
0

I think you wish to highlight the row. Please try following code, I think it might help:

Color color = dgv.Rows[prevRowIndex].DefaultCellStyle.SelectionBackColor;
dgv.Rows[curRowIndex].DefaultCellStyle.SelectionBackColor = color;
mihirj
  • 1,199
  • 9
  • 15
0

Try the following to change the current row. Since the OP is a little unclear as to what row should be the new row, my example simply shows moving from the current row to the previous row (if there is a previous row). The first line of code is optional. You can also hardcode col to 0 (or some other column) to use a fixed column if you don't want to use FullRowSelect.

dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
int row = dataGridView.CurrentCell.RowIndex;
int firstRow = dataGridView.Rows.GetFirstRow(DataGridViewElementStates.None);
if (row != firstRow)
{
  row--;
  int col = dataGridView.CurrentCell.ColumnIndex;
  dataGridView.CurrentCell = dataGridView[col, row];
}
Goal Man
  • 171
  • 2
  • 6
0

I came here wanting to learn how to programmatically select rows in a DataGridView control. Here is how to select the top row in your DataGridView control named dg1 and "click" it:

dg1.Rows[0].Selected = true;                      

dg1_RowHeaderMouseClick(null, null);

This then calls the following event which is expecting one selected row.

private void dg1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {                        
        var selectedRows = dg1.SelectedRows;

        // Make sure we have a single row selected
        int count = selectedRows.Count;

        if (count == 1)
        {                
            tbAssemblyName.Text = dg1.SelectedRows[0].Cells[0].Value.ToString();                
        }
    }

I had everything working when the user clicked the row they wanted. When there was only one record to choose from I wanted to "click" it for the user.

Grant Johnson
  • 317
  • 3
  • 8