manoj
manoj

Reputation: 5643

Selecting rows programmatically in DataGridView

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

Upvotes: 25

Views: 62257

Answers (4)

Grant Johnson
Grant Johnson

Reputation: 327

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.

Upvotes: 0

Goal Man
Goal Man

Reputation: 191

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];
}

Upvotes: 0

Alex Filipovici
Alex Filipovici

Reputation: 32561

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];

Upvotes: 59

mihirj
mihirj

Reputation: 1219

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;

Upvotes: 0

Related Questions