tags:

views:

33

answers:

1

Hi All,

I need the snippet code in C# to mantain selected the row from a DataGridView after that row is double clicked.

Right now I'm displaying data from a dataset and the selection mode is FullRowSelect. Any way to set this?

There are two scenarios to deal with:

  1. Everytime the timer ticks the selected row always go to the first row of datagridview.
  2. Once a row is clicked, it is selected but after the timer ticks the selected row goes to the first one.

Thanks for your help!

A newbie programmer

A: 

Right now I'm displaying data from a dataset and the selection mode is FullRowSelect. Any way to set this?

The DataGridView.SelectionMode property will do that for you through the DataGridViewSelectionMode enumeration.

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

As for the rest of your question, I think further details are required. What kind of behaviour are you after?

EDIT #1

As per your comment:

After I clicked in a row a new form is opened. The problem is that every time the timer is enabled the populate_DatagridView method is called and the selected row is located in the first row rather that keep selected the row clicked.

One solution could be the following:

private _dataGridViewRowSelectedIndex;

private void dataGridview1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {
    DataGridView dgv = (DataGridview)sender;
    if (dgv.Rows.GetRowState(e.RowIndex) == DataGridViewElementStates.Selected)
        _dataGridViewRowSelectedIndex = e.RowIndex;

    // Open your form here...

    // And when your form returns...
    // Set the selected index like so
    dgv.Rows[_dataGridViewRowSelectedIndex].Selected = true;
}

Does this help you out?

Will Marcouiller
After I clicked in a row a new form is opened. The problem is that every time the timer is enabled the populate_DatagridView method is called and the selected row is located in the first row rather that keep selected the row clicked.
Thanks for your answer but after reading a lot I think I can explain better my problem. My problem is that everytime I call the populate_datagridview method besides the actualization of the data, the first row of datagrid is selected rather than keep selected the current row or row clicked. Basically, what I need is to update the FirstDisplayedScrollingRowIndex to current row index.
That is basically what my code sample does, doesn't it? Is there any major difference? You perhaps got something I don't know about. Thanks for sharing if so. =)
Will Marcouiller