里面的DataGridView C#选择复选框 [英] selecting checkbox inside datagridview c#

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

问题描述

我有一个dowpdownlist,一个按钮,并在DataGridView内的复选框。



我仅仅只有手动创建的datagridview的复选框列。
(这里是代码)

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

下面是具体的步骤结果
第1步:用户将选择上项目复选框结果
第2步:用户将在下拉列表中选择项结果
步骤3:用户将点击按钮,它会改变ITEMNAME结果
上之前在DropDownList中选择的项目的复选框。



下面是点击按钮后,我的问题
,没有任何事情发生。

这里

是我的代码。

 私人无效的button1_Click(对象发件人,EventArgs的发送) 
{
INT X = 0;
的foreach(在this.DataGrid1.SelectedRows的DataGridViewRow项)
{
DataGridViewCheckBoxCell CHK =(DataGridViewCheckBoxCell)item.Cells [1];
如果(chk.Selected)
{
//这里的代码
}
,否则
{
//代码在这里
}
}
X = X + 1;
}


解决方案

* EDITED * *



我测试过这一点,它肯定工程。复制并粘贴到一个新的项目和它玩。它可以帮助你得到你需要的人。

 私人无效Form1_Load的(对象发件人,EventArgs五)
{

DataGridViewCheckBoxColumn复选框=新DataGridViewCheckBoxColumn(真);
checkBox.HeaderText =T / F;
dataGridView1.Columns.Add(复选框);

}

私人无效的button1_Click(对象发件人,EventArgs五)
{
的foreach(在的DataGridViewRow排dataGridView1.SelectedRows)
{

DataGridViewCheckBoxCell CHK =(DataGridViewCheckBoxCell)row.Cells [0];

如果(Convert.ToBoolean(chk.Value)==真)
{
MessageBox.Show(值是true);
}

}
}


i have a dowpdownlist, a button and a checkbox inside the datagridview.

i just only manually created a check box column on the datagridview. (here is the code)

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

here is the procedure.
step 1: the user will choose item on the checkbox.
step 2: the user will choose item on the dropdown.
Step 3: the user will click on the button and it will change the itemname
on the checkbox prior to the item selected on the dropdownlist.

here is my problem after clicking on the button, nothings happen.

here is my code.

private void button1_Click(object sender, EventArgs e)
        {
    int x = 0;
                foreach (DataGridViewRow item in this.DataGrid1.SelectedRows)
                {
                    DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)item.Cells[1];
                    if (chk.Selected)
                    {
                    // codes here
                    }
                    else
                    {
                    //code here
                    }
                }
                x = x + 1;
         }

解决方案

* EDITED **

I've tested this and it definitely Works. Copy and paste this into a new project and play with it. It should help you get to where you need to be.

   private void Form1_Load(object sender, EventArgs e)
    {

        DataGridViewCheckBoxColumn checkBox = new DataGridViewCheckBoxColumn(true);
        checkBox.HeaderText = "T/F";
        dataGridView1.Columns.Add(checkBox);

    }

    private void button1_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in dataGridView1.SelectedRows)
        {

            DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];

            if (Convert.ToBoolean(chk.Value) == true)
            {
                MessageBox.Show("Value Is True");
            }

        }
    }

这篇关于里面的DataGridView C#选择复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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