如图像这样 在dataagridview控件中有两个checkbox  X11和 X12 怎么才能实现单选功能呢?每行中x11和x12 中能有一个被选中。。请高手指点 ,,最好给点代码
   

解决方案 »

  1.   

    首先,绑定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就行。 
    (转载换分)
      

  2.   

    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;
    }
    }
    }
    }
      

  3.   

    应该要和两个radiobutton一样 同时只能选中一个。。
      

  4.   

    checkbox本来就不是用来实现单选的了,这个只能自己写代码控制了。
      

  5.   


            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能做的更好,没仔细的研究。希望对你有帮助,呵呵
      

  6.   

    urhp02857 不是实现动态添加行
      

  7.   

    怎么是动态添加行了……  你要的不就是同一行内有多个CheckBox的列只能选择其中的一个么 
      

  8.   

    数据当然是你自己添加啊…… 我只是给了你方法 DataGridView1.CellValueChanged+=new DataGridViewCellEventHandler(DataGridView1_CellValueChanged)
      

  9.   

    E恩 谢谢可以运行了不过有点毛病
    在第一次点了X11后 在点同一行的X12这是现实的时候会是两个都选了 只有在带你下一行的时候才能正确现实
    有没有什么方法可以实现动态的变化
      

  10.   

    为什么我一运行程序
    private void DataGridView1_CellValueChanged(System.Object sender, System.Windows.Forms.DataGridViewCellEventArgs 事件就报索引超出范围。必须为非负值并小于集合大小。这个错误??