【问题标题】:Right click to select row in dataGridView右键单击以选择dataGridView中的行
【发布时间】:2013-05-21 20:32:21
【问题描述】:

我需要在显示 ContextMenu 之前右键单击 dataGridView 中的一行,因为 contextMenu 是行相关的。

我试过了:

 if (e.Button == MouseButtons.Right)
        {

            var hti = dataGrid.HitTest(e.X, e.Y);
            dataGrid.ClearSelection();
            dataGrid.Rows[hti.RowIndex].Selected = true;
        }

或:

private void dataGrid_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            dataGrid.Rows[e.RowIndex].Selected = true;
            dataGrid.Focus();
        }
    }

这可行,但是当我尝试读取 dataGrid.Rows[CurrentRow.Index] 时,我只看到左键选择的行,而不是右键选择的行..

【问题讨论】:

    标签: c# select datagridview row right-click


    【解决方案1】:

    尝试像这样设置当前单元格(这将在选择上下文菜单项之前设置DataGridViewCurrentRow 属性):

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            var dataGrid = (DataGridView) sender;
            if (e.Button == MouseButtons.Right && e.RowIndex != -1)
            {
                var row = dataGrid.Rows[e.RowIndex];
                dataGrid.CurrentCell = row.Cells[e.ColumnIndex == -1 ? 1 : e.ColumnIndex];
                row.Selected = true;
                dataGrid.Focus();
            }
        }
    

    【讨论】:

      【解决方案2】:

      我意识到这个线程很旧,我只想添加一件事:如果您希望能够在多行上选择并执行操作:您可以检查您右键单击的行是否为已经选择了。这样,DataGridview 在这方面的行为就像 ListView。因此,右键单击尚未选择的行:选择该行并打开上下文菜单。右键单击已选择的行只会为您提供上下文菜单并按预期保持所选行。

       private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
          {
              if (e.RowIndex != -1 && e.ColumnIndex != -1)
              {
                  if (e.Button == MouseButtons.Right)
                  {
                      DataGridViewRow clickedRow = (sender as DataGridView).Rows[e.RowIndex]; 
                      if (!clickedRow.Selected)
                          dataGridView1.CurrentCell = clickedRow.Cells[e.ColumnIndex];
      
                      var mousePosition = dataGridView1.PointToClient(Cursor.Position);
      
                      ContextMenu1.Show(dataGridView1, mousePosition);
                  }
              }
          }
      

      【讨论】:

        【解决方案3】:
            private void grid_listele_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
            {
                if (e.Button == MouseButtons.Right)
                {
                    grid_listele.ClearSelection();
                    grid_listele[e.ColumnIndex, e.RowIndex].Selected = true;
                }
        
        
            }
        

        【讨论】:

        • 您能否解释一下为什么 OP 的示例不起作用以及您所做的工作?
        【解决方案4】:
         if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    var hti = GridView.HitTest(e.X, e.Y);
                    GridView.ClearSelection();
        
                    int index = hti.RowIndex;
        
                    if (index >= 0)
                    {
                        GridView.Rows[hti.RowIndex].Selected = true;
                        LockFolder_ContextMenuStrip.Show(Cursor.Position);
                    }
        
                }
        

        这是我猜的准确方法

        【讨论】:

          猜你喜欢
          • 2021-08-19
          • 2012-03-05
          • 2011-03-03
          • 2014-02-09
          • 1970-01-01
          • 2013-08-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多