我有一个数据 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/