【问题标题】:DataGridView - how can I make a checkbox act as a radio button?DataGridView - 如何使复选框充当单选按钮?
【发布时间】:2012-05-03 20:09:46
【问题描述】:

我有一个 Windows 窗体应用程序,它在 DataGridView 中显示对象列表。

此控件将布尔值呈现为复选框。

对象属性中有一组三个复选框,它们是互斥的。最多有一个可以是真的。因此,我希望复选框像一组单选按钮一样。

只是老家伙的旁白:我想现在的人们甚至不知道为什么这些被称为单选按钮。在过去,汽车中的收音机有 4 或 5 个按钮,按下其中任何一个都会导致所有其他按钮弹出。它们是相互排斥的。如今,“单选按钮”可能不再是一个有用的描述,因为我认为收音机不再有这样的按钮了。

我该怎么做?我想如果我将“CheckedChanged”事件附加到复选框,并且我知道该行,我将能够找到所有其他复选框。

我可以在第一次呈现复选框控件时挂钩什么事件,以便我可以将 CheckedChanged 事件附加到它?我知道DataGridView.CellFormatting,但我认为这是错误的,因为每次 DataGridView 绘制时都会调用它。我真的需要一个仅在第一次渲染 DGV 时调用的事件。

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    感谢 KeithS 提供的有用答案。

    当我在文档中查找 CellValueChanged 时,我发现了这个有用的信息:

    DataGridView.CellValueChanged 事件在提交用户指定的值时发生,通常在焦点离开单元格时发生。

    但是,对于复选框单元格,您通常希望立即处理更改。要在单击单元格时提交更改,您必须处理 DataGridView.CurrentCellDirtyStateChanged 事件。在处理程序中,如果当前单元格是复选框单元格,则调用 DataGridView.CommitEdit 方法并传入 Commit 值。

    这是我用来获取无线电行为的代码:

        void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            // Manually raise the CellValueChanged event
            // by calling the CommitEdit method.
            if (dataGridView1.IsCurrentCellDirty)
            {
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }
    
        public void dataGridView1_CellValueChanged(object sender,
                                                   DataGridViewCellEventArgs e)
        {
            // If a check box cell is clicked, this event handler sets the value
            // of a few other checkboxes in the same row as the clicked cell.
            if (e.RowIndex < 0) return; // row is sometimes negative?
            int ix = e.ColumnIndex;
            if (ix>=1 && ix<=3)
            {
                var row = dataGridView1.Rows[e.RowIndex];
    
                DataGridViewCheckBoxCell checkCell =
                    (DataGridViewCheckBoxCell) row.Cells[ix];
    
                bool isChecked = (Boolean)checkCell.Value;
                if (isChecked)
                {
                    // Only turn off other checkboxes if this one is ON. 
                    // It's ok for all of them to be OFF simultaneously.
                    for (int i=1; i <= 3; i++)
                    {
                        if (i != ix)
                        {
                            ((DataGridViewCheckBoxCell) row.Cells[i]).Value = false;
                        }
                    }
                }
                dataGridView1.Invalidate();
            }
        }
    

    【讨论】:

    • 非常感谢您的这篇文章;)
    【解决方案2】:

    您想要的是 DGV 本身上的 CellContentClick。附加一个处理程序,检查 DGV 的该列是否为 CheckBoxCell,如果是,则取消选中该行上的所有其他复选框。

    不过,请注意,对于 CheckBoxCell,此事件会在复选框值实际更改之前触发。这意味着无论您对当前单元格做什么,它都会被稍后触发的事件覆盖。摆脱这种情况的行为是,您可以通过选中行上的一个框然后再次检查它来检查行上的任何单元格(无论您是否尝试在处理程序中设置复选框值,复选框将在第二次单击后被清除)。要克服这一点并强制选中其中一个复选框,您可以改为处理 CellValueChanged,如果更改的单元格是当前单元格并且未选中,请检查它。

    【讨论】:

    • "您可以改为处理 CellValueChanged,如果更改的单元格是当前单元格并且未选中,请检查它"如何防止堆栈溢出?可以添加示例代码吗?
    【解决方案3】:
      private void dataGridViewProduit_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if ((sender as DataGridView).CurrentCell is DataGridViewCheckBoxCell)
            {
                if (Convert.ToBoolean(((sender as DataGridView).CurrentCell as DataGridViewCheckBoxCell).Value))
                {
                    foreach (DataGridViewRow row in (sender as DataGridView).Rows)
                    {
                        if (row.Index != (sender as DataGridView).CurrentCell.RowIndex && Convert.ToBoolean(row.Cells[e.ColumnIndex].Value) == true)
                        {
                            row.Cells[e.ColumnIndex].Value = false;
                        }
                    }
                }
            }
        }
    
        private void dataGridViewClient_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (this.dataGridViewClient.IsCurrentCellDirty)
            {
                dataGridViewClient.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }
    

    【讨论】:

      【解决方案4】:

      这里太容易了:

      请考虑您的复选框列是数据网格视图中的第二列。

      private void YourDatagridview_CellValueChanged(object sender, DataGridViewCellEventArgs e)
          {
               if (IsHandleCreated)
              {
                  if (YourDatagridview.CurrentCell == YourDatagridview.Rows[e.RowIndex].Cells[1])
                  {
                      if (Convert.ToBoolean(YourDatagridview.CurrentCell.Value) == true)
                      {
                          for (int i = 0; i < YourDatagridview.RowCount; i++)
                          {
                              if (YourDatagridview.Rows[i].Cells[1] != YourDatagridview.CurrentCell)
                              {
                                  YourDatagridview.Rows[i].Cells[1].Value = false;
                              }
                          }
                      }
                  }
              }
          }
      

      也叫这个:

      private void YourDatagridview_CurrentCellDirtyStateChanged(object sender, EventArgs e)
          {
              if (this.YourDatagridview.IsCurrentCellDirty)
              {
                  YourDatagridview.CommitEdit(DataGridViewDataErrorContexts.Commit);
              }
          }
      

      然后瞧!!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-10
        • 1970-01-01
        • 1970-01-01
        • 2014-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多