Click here to Skip to main content
14,449,842 members
Rate this:
Please Sign up or sign in to vote.
See more:
enable or disable context menu strip if data grid view selected cell

What I have tried:

private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
      {
          foreach (DataGridViewCell oneCell in dataGridView1.SelectedCells)
          {
              if (oneCell.Selected)
              {
                  contextMenuStrip1.Enabled = true;
              }
              else
              {
                  contextMenuStrip1.Enabled = false;
              }

          }
      }
Posted
Updated 13-Feb-20 3:50am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

This a formerly not a question, but I feel that you need to use the event arguments instead:
private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
   contextMenuStrip1.Enabled = dataGridView1
      .Rows[e.RowIndex]
      .Cells[e.ColumnIndex]
      .Selected;
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100