简体   繁体   中英

How do I get the previously selected cell in a DataGridView cell click event

I have a DataGridView and I am allowing only one column to be able to be selected.
I did this by checking, in the CellClick event, if the ColumnHeader for the selected cell matches, and, if it doesn't, I will clear the selection.
However, I want to make it that it will return to the previously selected cell if they selected a cell in the wrong Column. How can I do that?

I tried storing the selected cell object but thats not going to work because the moment the CellClick event fires, the selected cell object will change.

Also, for some reason my clear selection doesn't work everytime I click the wrong cell, especially if I click it really fast the wrong cell will still stay selected, for your info the DataGridView CellClick event is added dynamically during runtime and there are multiple DataGridView controls.

My code I can't provide the way I use to populate the DataGridView because it is really long.

  private void Form1_Load(object sender, EventArgs e)
  {
      DGV.CellClick += new DataGridViewCellEventHandler(DGV_CellClick);
  }

  void DGV_CellClick(Object sender, EventArgs e)
  {
      DataGridView dgv = (DataGridView)sender;
      // DataGridViewCell Selectedcell = dgv.SelectedCells[0];
      if (!dgv.Columns[dgv.SelectedCells[0].ColumnIndex].HeaderText.Contains("SCORE"))
          dgv.ClearSelection();
  }

Since you have just one Column enabled, using the SelectionChanged event, you can determine whether the DataGridView.CurrentCell belongs to the enabled Column and if it's not set the CurrentCell to the Cell of the enabled Column in the current Row:

int dgvEnabledColumn = 1;

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    var dgv = sender as DataGridView;
    var currentCell = dgv.CurrentCell;
    if (currentCell.ColumnIndex != dgvEnabledColumn )
    {
        dgv.CurrentCell = dgv[dgvEnabledColumn, currentCell.RowIndex];
    }
}

You could expand the enabled Columns' selection using a collection of Columns (to set the current cell to the nearest/previous/next Column).

For example, using a collection of DataGridViewColumn objects, determine if the Current cell is enabled and, if it's not, set the Current cell to the nearest next enabled Column, if any, or the nearest previous one:

The List<DataGridViewColumn> can be filled after the DataGridView is initialized:

enabledColumns = new List<DataGridViewColumn>()
{
    dataGridView1.Columns[0],
    dataGridView1.Columns[2]
};

List<DataGridViewColumn> enabledColumns = null;

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    if (enabledColumns is null) return;
    var dgv = sender as DataGridView;
    var currentCell = dgv.CurrentCell;
    if (!enabledColumns.Any(c => c.Index == currentCell.ColumnIndex))
    {
        var nextCol = enabledColumns.FirstOrDefault(c => c.Index > currentCell.ColumnIndex);
        if (nextCol != null) {
            dgv.CurrentCell = dgv[nextCol.Index, currentCell.RowIndex];
        }
        else
        {
            var previousCol = enabledColumns.First(c => c.Index < currentCell.ColumnIndex);
            dgv.CurrentCell = dgv[previousCol.Index, currentCell.RowIndex];
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM