日期:2014-05-18  浏览次数:21289 次

winform 在DataGridview中的checkbox如何实现单选?
winform 在DataGridview中的checkbox如何实现单选?

------解决方案--------------------
http://blog.csdn.net/zhoukk1985/article/details/7229257
------解决方案--------------------
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1 || e.ColumnIndex == -1)
return;


if (e.ColumnIndex == 0)
{
DataGridViewCheckBoxCell ccbc = dataGridView1.Rows[e.RowIndex].Cells[0] as DataGridViewCheckBoxCell;
if (ccbc.Value.ToString() == "true")
{
ccbc.Value = "false";
}
else
{
ccbc.Value = "true";
}
}
}

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow dr in this.dataGridView1.Rows)
{
//dr.ReadOnly = true;
DataGridViewCheckBoxCell dc = dr.Cells[0] as DataGridViewCheckBoxCell;
if (dc == null)
return;
dc.ReadOnly = false;
dc.TrueValue = "true";
dc.FalseValue = "false";
dc.Value = "false";
}
}
这是DataGridview的两个事件,dr.Cells[0]是第一列"选择"
------解决方案--------------------
http://blog.csdn.net/zhoukk1985/article/details/7229257
------解决方案--------------------
如果有多个含有Checkbox类型的列,需要在网格数据更改时进行判断,保证选中一个后其他的自动变为非选中。
由于Checkbox只有选中、非选中两种状态(忽略第三态),可以在CellBeginEdit事件中处理。
------解决方案--------------------
我这台电脑没有VS,知道datagridview中属性中有个叫"集合"的点进去,里面可以设置列为checkbox,到里面看下其他属性。