【问题标题】:Datagridview forcing only one checkbox to be selected in a columnDatagridview 强制在一列中只选择一个复选框
【发布时间】:2012-07-13 08:05:10
【问题描述】:

如何强制在 Datagridview 的列中仅选中一个复选框?

【问题讨论】:

  • 为了与整个计算机软件行业保持一致,请使用单选按钮而不是复选框。列表复选框意味着您可以选择多个项目,而单选按钮意味着您只能为组选择一个。可以解决您的问题,但我强烈建议您不要这样做。如果因为不允许值而选择复选框而不是单选按钮,请添加单选按钮“无”或“清除”按钮以不选择任何选项。
  • @SteveB 我知道我来晚了,但我在 Winforms Datagridview 中没有看到单选按钮?

标签: c# .net winforms datagridview checkbox


【解决方案1】:
 private void grdRegClass_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (grdRegClass.Columns.IndexOf(grdRegClass.Columns["Status"]) == e.ColumnIndex)
        {
            int currentcolumnclicked = e.ColumnIndex;
            int currentrowclicked = e.RowIndex;
            foreach (DataGridViewRow dr in grdRegClass.Rows)
            {
                dr.Cells[currentcolumnclicked].Value = false;
            }
            grdRegClass.CurrentRow.Cells[currentrowclicked].Value = true;  
        }
    }

【讨论】:

    【解决方案2】:

    您必须订阅网格的 CellValueChanged 事件,并根据当前单元格的检查状态,循环 DataGridView 并将 true/false 设置为其他单元格的值。

    void grd_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
          if ((sender as DataGridView).CurrentCell is DataGridViewCheckBoxCell)
          {
               if (Convert.ToBoolean(((sender as DataGridView).CurrentCell as DataGridViewCheckBoxCell).Value))
               {
                       // Maybe have a method which does the
                        //loop and set value except for the current cell
                }
            }
    }
    

    【讨论】:

    • 客户端解决方案(jquery 可以帮助很多)更可取,这将避免数十次回发
    • @SteveB 检查它的标签 winform
    • @V4Vendetta 只是一个问题,为什么是 CellValueChanged 而不是 CellContentClick?据我了解,当任何单元格的内容发生更改时会引发 CellValueChanged 事件,但是当仅单击单元格中的特定内容时会引发 CellContentClick 事件(IE:在这种情况下为复选框),因此不会使用 CellContentClick 事件更可取?
    • @SokwhanHuh 只有在值更改后才会起作用,内容单击可能会在单元格仍在编辑时运行多次,但一旦编辑完成并通过 ValueChanged 触发,因此更有意义在那里处理。 (这也不是唯一的方法,而是一种方法)
    • CellValueChanged 方法适用于任何类型的单元格,但 CellContentClick 仅适用于有效网格。
    【解决方案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】:

      您可以使用DGVCellEndEdit 事件,因为它发生在修改单元格之后。请阅读以下代码 sn-p 中的 cmets 以使用以下代码:

      private void dgrvUserProfileView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
      {
          int CheckedCount = 0;
      
          //Make sure you have set True Value/ false Value property of check box column to 1/0 or true/false resp.
          //Lets say your column 5th(namely Department) is a checked box column
          if (dgrvUserProfileView.Columns[e.ColumnIndex].Name == "Department")
          {
              for (int i = 0; i <= dgrvUserProfileView.Rows.Count - 1; i++)
              {
                  if (Convert.ToBoolean(dgrvUserProfileView.Rows[i].Cells["Department"].Value) == true)
                  {
                      CheckedCount = CheckedCount + 1;
                  }
              }
      
              if (CheckedCount == 1)
              {
                  for (int i = 0; i <= dgrvUserProfileView.Rows.Count - 1; i++)
                  {
                      if (Convert.ToBoolean(dgrvUserProfileView.Rows[i].Cells["Department"].Value) == true)
                      {
                          dgrvUserProfileView.Rows[i].Cells["Department"].ReadOnly = true;
                      }
                  }
              }
              else
              {
                  for (int i = 0; i <= dgrvUserProfileView.Rows.Count - 1; i++)
                  {
                      dgrvUserProfileView.Rows[i].Cells["Department"].ReadOnly = false;
                  }
              }
          }
      }
      

      希望这会有所帮助!

      【讨论】:

      【解决方案5】:
      private void dgvlist_CellContentClick(object sender, DataGridViewCellEventArgs e)  
          {  
              int currentcolumnclicked = e.ColumnIndex;  
              for (int i = 0; i <= dgvlist.Columns.Count - 1; i++)  
              {  
                  if (dgvlist.Columns[i] is DataGridViewCheckBoxColumn)  
                  {  
                      if (Convert.ToString(dgvlist.CurrentRow.Cells[i].EditedFormattedValue) == "True" && i !=currentcolumnclicked)  
                      {  
                          dgvlist.CurrentRow.Cells[i].Value = false;  
                      }  
                  }  
              }  
          }  
      

      【讨论】:

        【解决方案6】:
        private void dgvCaixa_CellContentClick(object sender, DataGridViewCellEventArgs e)
                {
                    if ((sender as DataGridView).CurrentCell is DataGridViewCheckBoxCell)
                    {
                        foreach (DataGridViewRow row in dgvCaixa.Rows)
                        {
                            if (row.Index != dgvCaixa.CurrentCell.RowIndex && Convert.ToBoolean(row.Cells[e.ColumnIndex].Value) == true)
                            {
                                row.Cells[e.ColumnIndex].Value = false;
                            }
                        }
                    }
                }
        

        【讨论】:

          【解决方案7】:

          在 vb.net 中:

          Private Sub DataGridViewJobsList_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewJobsList.CellValueChanged
              If TypeOf TryCast(sender, DataGridView).CurrentCell Is DataGridViewCheckBoxCell Then
                  Dim cell1 As DataGridViewCell
                  cell1 = TryCast(sender, DataGridView).CurrentCell
                  If cell1.Value = True Then
                      For Each row As DataGridViewRow In TryCast(sender, DataGridView).Rows
                          If row.Index <> cell1.RowIndex AndAlso row.Cells(e.ColumnIndex).Value = "1" Then
                              row.Cells(e.ColumnIndex).Value = False
                          End If
                      Next
                  End If
              End If
          End Sub
          
          Private Sub DataGridViewJobsList_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridViewJobsList.CurrentCellDirtyStateChanged
              If Me.DataGridViewJobsList.IsCurrentCellDirty Then
                  DataGridViewJobsList.CommitEdit(DataGridViewDataErrorContexts.Commit)
              End If
          End Sub
          

          【讨论】:

            【解决方案8】:

            您可以在 DGV 上将 VirtualMode 设置设置为 TRUE 以仅允许一个复选框。您可以在 DatagridView 上将 VirtualMode 设置为 FALSE 以检查多个。

            我认为这是解决问题的最简单方法。

            【讨论】:

              【解决方案9】:

              不用担心 Column 并且永远不会失败,试试这个:

              private void DgvIVA_CellContentClick(object sender, DataGridViewCellEventArgs e)
              {
                  DataGridView dg = (DataGridView)sender;
                  if (dg.Rows.Count == 0) return;
                  if (dg.Rows[e.RowIndex].Cells[e.ColumnIndex].GetType() == typeof(DataGridViewCheckBoxCell))
                  {
                      int rowSelIndex = e.RowIndex;
                      foreach (DataGridViewRow row in dg.Rows)
                      {
                          if (row.Index != rowSelIndex)
                          {
                              row.Cells[e.ColumnIndex].Value = false;
                          }
                      }
                  }
              }
              

              【讨论】:

                【解决方案10】:
                private void dataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e)
                        {
                            if (e.ColumnIndex == 1)
                            {
                                try
                                {
                                    string val = dataGridView3.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                                    if (val == "False")
                                        val = "True";
                                    else if (val == "True")
                                        val = "False";
                                    dataGridView3.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = val;
                
                                for (int i = 0; i < dataGridView3.Rows.Count; i++)
                                {
                                    string active = "";
                                    if (i != e.RowIndex)
                                    {
                                        if (val == "False")
                                        {
                                            dataGridView3.Rows[i].Cells[1].Value = "True";
                                            active = "Y";
                                        }
                                        else if (val == "True")
                                        {
                                            dataGridView3.Rows[i].Cells[1].Value = "False";
                                            active = "N";
                                        }
                                    }
                                    else
                                    {
                                        if (val == "False")
                                            active = "N";
                                        else
                                            active = "Y";
                                    }
                
                
                
                
                                }
                
                
                            }
                            catch (Exception ex)
                            { }
                        }
                    }
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2016-05-22
                  • 1970-01-01
                  • 2017-08-10
                  • 1970-01-01
                  • 2013-02-26
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多