【发布时间】:2020-06-02 10:55:38
【问题描述】:
我创建了一个应用程序,它显示一个带有一系列问题的DataGridView。
dgv 结构由一个用于问题文本的字符串列和三个用于答案的布尔/复选框列组成(是、否、N/A)。
每个问题都显示在自己的行中。
我希望我的程序只允许用户在每一行上只选择是、只选择否或只选择 N/A。
我想我需要在选中一个选项时取消选中其他复选框选项,但我不太确定如何执行此操作。
我已经设置了 CellValueChanged 和 CellContentClick 事件,但我不确定实现所需功能所需的代码。
DataGridView 绑定到 DataTable。
我目前拥有的代码:
private void dgvQuestions_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
int columnIndex = e.ColumnIndex;
int rowIndex = e.RowIndex;
DataGridViewCheckBoxCell chkYes = dgvQuestions.Rows[rowIndex].Cells[2] as DataGridViewCheckBoxCell;
DataGridViewCheckBoxCell chkNo = dgvQuestions.Rows[rowIndex].Cells[3] as DataGridViewCheckBoxCell;
DataGridViewCheckBoxCell chkNA = dgvQuestions.Rows[rowIndex].Cells[4] as DataGridViewCheckBoxCell;
if (Convert.ToBoolean(chkYes.Value) == true)
{
}
if (Convert.ToBoolean(chkNo.Value) == true)
{
}
if (Convert.ToBoolean(chkNA.Value) == true)
{
}
}
private void dgvQuestions_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
dgvQuestions.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
【问题讨论】:
-
我
-
Programmatically check a DataGridView CheckBox that was just unchecked。顺便说一句,与 DataGridView 相关的问题要求您指定 DataSource 类型。
-
@Jimi,我不同意,因为 BindingList
是通用的,可以用作 DataGridView 的数据源(此时类 T 的所有公共属性在 DataGridView 中可见并自动填充)。所以,我的观点是这样的问题可以用通用的形式来回答,因为它适用于任何类 T。 -
@IVSoftware 是的,BindingList 可以做它可以做的事情。 DataTable 可以做其他事情。绑定到 BindingSource 的 DataTable 等等。我的观点:使用 DataTable(例如),所需要的只是一个 calibrated 表达式(即使使用计算的列)。使用 BindingList 和类对象:类是否实现
INotifyPropertyChannged?和/或,这个逻辑是否可以在类对象本身中实现,所以 Single Option 实际上是在内部触发的,您根本不会打扰 UI 层?如果您可以避免在视图方面工作,那是一件好事。等 -
当然!我的观点非常狭窄:BindingList、DataTable 很容易为类 T 实现,因此不需要在发布时知道。 DataTable from Constrained Interface 或 DataTable from IEnumerable<T>。我认为你和我在这个问题上大多站在同一边。实际上,它是在 MyDataGridView 类内部处理的:DataGridView{}
标签: c# .net winforms datagridview datagridviewcolumn