选择/取消对datagridview的一个复选框 [英] Check/Uncheck a checkbox on datagridview

查看:174
本文介绍了选择/取消对datagridview的一个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我,为什么它不工作?
我有一个复选框,如果我点击它,
这应该取消在DataGridView内所有的复选框被它包括用户选中的复选框前检查。

下面是code:

 私人无效chkItems_CheckedChanged(对象发件人,EventArgs的发送)
        {
            的foreach(在datagridview1.Rows的DataGridViewRow行)
            {
                DataGridViewCheckBoxCell CHK =(DataGridViewCheckBoxCell)row.Cells [1];
                如果(chk.Selected ==真)
                {
                    chk.Selected = FALSE;
                }
                其他
                {
                    chk.Selected = TRUE;
                }
            }
        }

复选框不应选择。它应该被检查。

这里是添加的列

  DataGridViewCheckBoxColumn CheckboxColumn =新DataGridViewCheckBoxColumn();
            复选框CHK =新的复选框();
            CheckboxColumn.Width = 20;
            datagridview1.Columns.Add(CheckboxColumn);


解决方案

望着这<一个href=\"http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/74df79a9-87ad-4cec-8502-3a357015558d\">MSDN论坛发帖的它表明单元格的值与<一个比较href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcheckboxcell.truevalue.aspx\">Cell.TrueValue.

所以,由它去的例子您code应该看起来像这样:(这是完全未经测试的)

编辑:看来,对于Cell.TrueValue为未绑定DataGridViewCheckBox默认为空,你需要设置它在列定义

 私人无效chkItems_CheckedChanged(对象发件人,EventArgs的发送)
{
    的foreach(在dataGridView1.Rows的DataGridViewRow行)
    {
        DataGridViewCheckBoxCell CHK =(DataGridViewCheckBoxCell)row.Cells [1];
        如果(chk.Value == chk.TrueValue)
        {
            chk.Value = chk.FalseValue;
        }
        其他
        {
            chk.Value = chk.TrueValue;
        }
    }
}


这code工作笔记设置T​​rueValue和FalseValue在构造加上还检查空:

 公共部分Form1类:表格
{
    公共Form1中()
    {
        的InitializeComponent();
        DataGridViewCheckBoxColumn CheckboxColumn =新DataGridViewCheckBoxColumn();
        CheckboxColumn.TrueValue =真;
        CheckboxColumn.FalseValue = FALSE;
        CheckboxColumn.Width = 100;
        dataGridView1.Columns.Add(CheckboxColumn);
        dataGridView1.Rows.Add(4);
    }    私人无效chkItems_CheckedChanged(对象发件人,EventArgs的发送)
    {
        的foreach(在dataGridView1.Rows的DataGridViewRow行)
        {
            DataGridViewCheckBoxCell CHK =(DataGridViewCheckBoxCell)row.Cells [0];
            如果(chk.Value == || chk.FalseValue == chk.Value空)
            {
                chk.Value = chk.TrueValue;
            }
            其他
            {
                chk.Value = chk.FalseValue;
            }        }
        dataGridView1.EndEdit();
    }
}

Can someone help me why it doesn't work? I have a checkbox and if I click on it, this should uncheck all the checkbox inside the datagridview which were checked before including the user selected checkbox.

Here is the code:

        private void chkItems_CheckedChanged(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in datagridview1.Rows)
            {
                DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[1];
                if (chk.Selected == true)
                {
                    chk.Selected = false;
                }
                else
                {
                    chk.Selected = true;
                }
            }
        }

the checkbox should not be selected. it should be checked.

here is the added column

            DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();
            CheckBox chk = new CheckBox();
            CheckboxColumn.Width = 20;
            datagridview1.Columns.Add(CheckboxColumn);

解决方案

Looking at this MSDN Forum Posting it suggests comparing the Cell's value with Cell.TrueValue.

So going by its example your code should looks something like this:(this is completely untested)

Edit: it seems that the Default for Cell.TrueValue for an Unbound DataGridViewCheckBox is null you will need to set it in the Column definition.

private void chkItems_CheckedChanged(object sender, EventArgs e)
{
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[1];
        if (chk.Value  == chk.TrueValue)
        {
            chk.Value = chk.FalseValue;
        }
        else
        {
            chk.Value = chk.TrueValue;
        }
    }
}


This code is working note setting the TrueValue and FalseValue in the Constructor plus also checking for null:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();
        CheckboxColumn.TrueValue = true;
        CheckboxColumn.FalseValue = false;
        CheckboxColumn.Width = 100;
        dataGridView1.Columns.Add(CheckboxColumn);
        dataGridView1.Rows.Add(4);
    }

    private void chkItems_CheckedChanged(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
            if (chk.Value == chk.FalseValue || chk.Value == null)
            {
                chk.Value = chk.TrueValue;
            }
            else
            {
                chk.Value = chk.FalseValue;
            }

        }
        dataGridView1.EndEdit();
    }
}

这篇关于选择/取消对datagridview的一个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆