首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查DatagridviewCheckboxCell - c#

检查DatagridviewCheckboxCell - c#
EN

Stack Overflow用户
提问于 2016-05-25 17:16:08
回答 1查看 188关注 0票数 0

我有3个DatagridviewCheckboxCell,我使用下面的代码来检查它们:

代码语言:javascript
复制
foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            DataGridViewCheckBoxCell chk1 = (DataGridViewCheckBoxCell)row.Cells["Column6"];
            DataGridViewCheckBoxCell chk2 = (DataGridViewCheckBoxCell)row.Cells["Column7"];
            DataGridViewCheckBoxCell chk3 = (DataGridViewCheckBoxCell)row.Cells["Column8"];

            string commande2 = "SELECT id_pres from Fonct_Prest where id_fonc = @id";
            OleDbCommand cmd2 = new OleDbCommand(commande2, con);
            cmd2.Parameters.AddWithValue("@id", row.Cells["Code"].Value);
            dt2.Load(cmd2.ExecuteReader());

            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                if (dt2.Rows[i][0].ToString() == "1")
                {
                    chk1.Value = true;
                }
                if (dt2.Rows[i][0].ToString() == "2")
                {
                    chk2.Value = true;
                }
                if (dt2.Rows[i][0].ToString() == "6")
                {
                    chk3.Value = true;
                }
            }
            cmd2.Parameters.Clear();
            dt2.Rows.Clear();
        }

这段代码运行良好,但在datagridview的第一行中,复选框未被选中。我的代码中的错误在哪里?

EN

回答 1

Stack Overflow用户

发布于 2016-05-25 17:36:49

如果你知道列名,你可以试试这段代码。

代码语言:javascript
复制
    private void button2_Click(object sender, EventArgs e)
    {
        for(int i = 0; i < dataGridView1.RowCount; i++)
        {
            //Set defined value
            //dataGridView1.Rows[i].Cells["Column2"].Value = true;

            //toggle value from checked to unchecked.
            dataGridView1.Rows[i].Cells["Column2"].Value = !Convert.ToBoolean(dataGridView1.Rows[i].Cells["Column2"].Value);
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37432667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档