开发者

Keep row of datagridview clicked by user selected

开发者 https://www.devze.com 2023-01-04 01:37 出处:网络
I need the snippet code in C# to mantain selected the row from a DataGridView after that row is do开发者_StackOverflow社区uble clicked.

I need the snippet code in C# to mantain selected the row from a DataGridView after that row is do开发者_StackOverflow社区uble 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


You must do it in the function timer_tick

private void timer3_Tick(object sender, EventArgs e)
    {
        int rowIndex;

        if (dgvOrdini.Rows.Count == 0)  //here I check if the dgv is empty
            rowIndex = 0;
        else
        // I save the index of the current row in rowIndex
        rowIndex = this.dgvOrdini.CurrentCell.RowIndex;  

        .......
        .......
        if (dgvOrdini.Rows.Count != 0)  //Now if the dgv is not empty
           //I set my rowIndex
           dgvOrdini.CurrentCell = dgvOrdini.Rows[rowIndex].Cells[0];    
    }

With this method the selected row will not change.


Try this. First save the index of row selected actually

int index = -1 //set the index to negative (because if you have only 1 row in your grid, this is a zero index
if (yourdatagridview.Rows.Count > 0) //if you have rows in datagrid
{
 index = yourdatagridview.SelectedRows[0].Index; //then save index into variable
}

now you can update the datagridview...

After update you must set the selecvted row:

if (index != -1) //if index == -1 then you don't have rows in your datagrid
{
 yourdatagridview.Rows[index].Selected = true; 
}

It's works !


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消