我的DataGridView绑定了一个对象,对象中有bool值,所以在DataGridView中有一列被自动生成了checkbox类型的复选框。
问题:我想在checkbox上,只能选中一个checkbox,而不是多选。
请问怎么实现
DataGridView形式:
Name(文本框)    选择(复选框)     备注(文本框) 
张三                               无
李四               √              无
王五                                                 
可以添加行
我的思路for (int i = 0; i < this.datagridview1.Rows.Count; i++)
           {
               if(this.datagridview1.Rows[e.RowIndex].Cells[1].Value.ToString() == "True")
               {
                     //其他的 checkbox不能选,不会实现
                }
           }请指点。

解决方案 »

  1.   


               for (int i = 0; i < this.datagridview1.Rows.Count; i++)
               {
                   if(i <> e.RowIndex)
                   {
                        this.datagridview1.Rows[i].Cells[1].Value = "false";
                   }
               }
      

  2.   

    cellclick事件下
    for (int i = 0; i < this.datagridview1.Rows.Count; i++)
               {
                  if(this.datagridview1.Rows[i].Cells[1].Value.ToString() == "True")
                   {
                        if(i <> e.RowIndex)
                   {
                        this.datagridview1.Rows[e.RowIndex].Cells[1].Value = "false";
                   }
                    }           }
      

  3.   


               for (int i = 0; i < this.datagridview1.Rows.Count; i++)
               {
                   if(i <> e.RowIndex)
                   {
                        this.datagridview1.Rows[i].Cells[1].Value = false;
                   }
               }
      

  4.   

    直接把你的代码替换成下面的就OK
            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                for (int i = 0; i < this.datagridview1.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell cbh = (DataGridViewCheckBoxCell)this.dgvCommon.Rows[count].Cells[1];
                    this.datagridview1.Rows[count].Cells[1].Value = false;
                }
                DataGridViewCheckBoxCell cbh = (DataGridViewCheckBoxCell)this.dgvCommon.Rows[e.RowIndex].Cells[1];
                this.dataGridView1.Rows[e.RowIndex].Cells[1].Value = true;
            }
      

  5.   

    再来一个
            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                for (int i = 0; i < this.datagridview1.Rows.Count; i++)
                {
                    if (count != e.RowIndex)
                    {
                        DataGridViewCheckBoxCell cbh = (DataGridViewCheckBoxCell)this.dgvCommon.Rows[count].Cells[1];
                        this.datagridview1.Rows[count].Cells[1].Value = false;
                    }
                    else
                    {
                        DataGridViewCheckBoxCell cbh = (DataGridViewCheckBoxCell)this.dgvCommon.Rows[e.RowIndex].Cells[1];
                        this.dataGridView1.Rows[e.RowIndex].Cells[1].Value = true;
                    }
                }
            }
      

  6.   

    还有个问题:
    在dgvDisplay_CellEndEdit事件里this.dataGridView1.EndEdit();
                if (string.IsNullOrEmpty(this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString()) &&
                    (e.RowIndex != this.dataGridView1.Rows.Count - 1))
                {
                    this.dataGridView1.Rows.Remove(this.dataGridView1.Rows[e.RowIndex]);
                }
    如果Name里的内容删除,当编辑完单元格失去焦点时,该行要删除,怎么实现,上面是我的代码,不过出错了。报:未将对象实例化。
    请解决
      

  7.   

    编辑完后在单元格失去焦点时就调用
    this.dataGridView1.EndEdit();
    然后再在dgvDisplay_CellEndEdit事件里写你其它的删除代码
    你试试
      

  8.   

    谢谢你的回复。
    我在CellLeave事件里已经加了this.dataGridView1.EndEdit();
    在dataGridView1_CellEndEdit事件里,也加了this.dataGridView1.EndEdit();
    都不行,都是报:未将对象实例化。 还有个问题,不知你在#6和#8楼上,使用的count是什么值?是this.datagridview1.Rows.Count这个吗?
    dgvCommon应该是datagridview1吧