c# - 在 DataGridView 中只允许选中一个复选框

标签 c# sql winforms checkbox datagridview

我有一个数据 GridView ,其中填充了来自数据库的数据。第一列是一个复选框列(从数据库中检索到的该列的数据是 BIT 类型),我希望用户只选中一个。如果用户选择其他,则必须取消选中第一个。

我看过很多代码,但没有一个有效。

我能做什么?

是带有 SQL SERVER 的 Winforms C# 应用程序。

最佳答案

private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
   // Whatever index is your checkbox column
   var columnIndex = 0;
   if (e.ColumnIndex == columnIndex)
   {
      // If the user checked this box, then uncheck all the other rows
      var isChecked = (bool)dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
      if (isChecked)
      {
         foreach (DataGridViewRow row in dataGridView.Rows)
         {
            if (row.Index != e.RowIndex)
            {
               row.Cells[columnIndex].Value = !isChecked;
            }
         }
      }
   }
}

关于c# - 在 DataGridView 中只允许选中一个复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35350584/

相关文章:

c# - 将事件写入事件日志期间出现 WCF 异常

c# - 使用 BouncyCaSTLe C# 库从 PGP 加密数据中识别收件人 KeyId

mysql - mysql WHERE ... IN 的性能

sql - 检查 Postgres jsonb 是否包含以下值之一

MySQL : Cannot add or update a child row: a foreign key constraint fails

c# - 如何从 Windows 窗体将单个文件发布到 asp.net webform?

vb.net - 如何创建显示绑定(bind)到对象列表的百分比的条形图?

c# - 如何使用 NuGet 打包命令行工具?

c# - 我可以将这三个相似的函数合并为一个函数吗?

c# - "The type does not include any accessible constructors"- 具有无参数构造函数