DataGridViewCheckBoxColumn怎么实现单选?? 如图像这样 在dataagridview控件中有两个checkbox X11和 X12 怎么才能实现单选功能呢?每行中x11和x12 中能有一个被选中。。请高手指点 ,,最好给点代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 首先,绑定DataGridView的某列是bit型的应该知道,就是说在DataGridView显示的CheckBox框的字段应该知道,我这里设其为“AA”. private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { this.dataGridView1.AllowUserToAddRows = false;//这句话在窗体加载时加上也行,也可以修改属性 for (int i = 0; i < this.dataGridView1.RowCount; i++) { if (this.dataGridView1.Rows[i].Cells["AA"].Value.ToString() == "True") { this.dataGridView1.Columns["AA"].ReadOnly = true; } } }注:不知道这个意思是否和你说的一样?我的基本思路就是:在改变checkbox值的时候,只要有一个是选中,其余的就不可用,如果想可以用的话再设置为False就行。 (转载换分) private int i= 0;private void DataGridView1_CellValueChanged(System.Object sender, System.Windows.Forms.DataGridViewCellEventArgs e){ if (e.ColumnIndex == i) { bool isChecked = Convert.ToBoolean(this.DataGridView1(e.ColumnIndex, e.RowIndex).Value); if (isChecked) { foreach (DataGridViewRow row in this.DataGridView1.Rows) { row.Cells(e.ColumnIndex).Value = false; } } }} 应该要和两个radiobutton一样 同时只能选中一个。。 checkbox本来就不是用来实现单选的了,这个只能自己写代码控制了。 private void DataGridView1_CellValueChanged(System.Object sender, System.Windows.Forms.DataGridViewCellEventArgs e) { DataGridViewCell cellChange = this.DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; if (cellChange.GetType() == typeof (DataGridViewCheckBoxCell)) { bool bChecked = Convert.ToBoolean(cellChange.Value.ToString()); if (bChecked) { foreach (DataGridViewCell cell in this.DataGridView1.Rows[e.RowIndex].Cells) { if (cell.GetType() == typeof(DataGridViewCheckBoxCell) && cell != cellChange) cell.Value = false; } } } else if (cellChange.GetType() == typeof (DataGridViewTextBoxCell)) { } }虽然能实现你所说的功能,但是写在CellValueChanged事件下必须要单元格失去焦点的时候才能触发事件,所以在显示上有点不友好。可能通过重写CellValueChanged能做的更好,没仔细的研究。希望对你有帮助,呵呵 urhp02857 不是实现动态添加行 怎么是动态添加行了…… 你要的不就是同一行内有多个CheckBox的列只能选择其中的一个么 数据当然是你自己添加啊…… 我只是给了你方法 DataGridView1.CellValueChanged+=new DataGridViewCellEventHandler(DataGridView1_CellValueChanged) E恩 谢谢可以运行了不过有点毛病在第一次点了X11后 在点同一行的X12这是现实的时候会是两个都选了 只有在带你下一行的时候才能正确现实有没有什么方法可以实现动态的变化 为什么我一运行程序private void DataGridView1_CellValueChanged(System.Object sender, System.Windows.Forms.DataGridViewCellEventArgs 事件就报索引超出范围。必须为非负值并小于集合大小。这个错误?? 关于Windows Form程序禁用快捷键 能不能在字符串中插入控件? ListView控件中的右键菜单问题 c#怎么操作数据库可写性 在C#中声明一个常量数组该怎么写? gridview高级应用 !!!!!!!!来看看问题,绝对你没遇到过..!!!!!! 关于dataGrid1中某行如何获得或失去选中状态的问题 C#简单问题? 如何查看所有与数据库的transaction连接 100 分请教!!关于 多线程 的 小问题? C#做WINFORM中内嵌we浏览器打开网站后,遇弹出式窗口提示重新身份验证,如何解决,请各位高手指点 将gridview的内容写入oracle数据库出错
{
this.dataGridView1.AllowUserToAddRows = false;//这句话在窗体加载时加上也行,也可以修改属性
for (int i = 0; i < this.dataGridView1.RowCount; i++)
{
if (this.dataGridView1.Rows[i].Cells["AA"].Value.ToString() == "True")
{
this.dataGridView1.Columns["AA"].ReadOnly = true;
}
}
}注:不知道这个意思是否和你说的一样?我的基本思路就是:在改变checkbox值的时候,只要有一个是选中,其余的就不可用,如果想可以用的话再设置为False就行。
(转载换分)
private void DataGridView1_CellValueChanged(System.Object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == i) {
bool isChecked = Convert.ToBoolean(this.DataGridView1(e.ColumnIndex, e.RowIndex).Value);
if (isChecked) {
foreach (DataGridViewRow row in this.DataGridView1.Rows) {
row.Cells(e.ColumnIndex).Value = false;
}
}
}
}
private void DataGridView1_CellValueChanged(System.Object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
{
DataGridViewCell cellChange = this.DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (cellChange.GetType() == typeof (DataGridViewCheckBoxCell))
{
bool bChecked = Convert.ToBoolean(cellChange.Value.ToString());
if (bChecked)
{
foreach (DataGridViewCell cell in this.DataGridView1.Rows[e.RowIndex].Cells)
{
if (cell.GetType() == typeof(DataGridViewCheckBoxCell) && cell != cellChange)
cell.Value = false;
}
}
}
else if (cellChange.GetType() == typeof (DataGridViewTextBoxCell))
{ }
}虽然能实现你所说的功能,但是写在CellValueChanged事件下必须要单元格失去焦点的时候才能触发事件,所以在显示上有点不友好。可能通过重写CellValueChanged能做的更好,没仔细的研究。希望对你有帮助,呵呵
在第一次点了X11后 在点同一行的X12这是现实的时候会是两个都选了 只有在带你下一行的时候才能正确现实
有没有什么方法可以实现动态的变化
private void DataGridView1_CellValueChanged(System.Object sender, System.Windows.Forms.DataGridViewCellEventArgs 事件就报索引超出范围。必须为非负值并小于集合大小。这个错误??