DatagridView 复选框列始终为空。

5
我有一个奇怪的问题。基本上,我有一个数据网格视图和一个按钮。当我单击此按钮时,它检查所有行的第一列值 - 复选框列。然后根据当前状态将其设置为true / false。
这一切都很好。
但是,然后,我有另一个按钮来处理这些被勾选的行。我点击它,它只会识别第一行为被选中的。其余的似乎现在为空..?
那么,如何以编程方式设置数据网格视图中复选框列的值,然后再次读取它,因为根据我的结果,我显然偏离了正确方向。
这将设置复选框并且我可以看到它们,手动取消勾选等。
foreach (DataGridViewRow row in dgv.Rows)
        {
            var ch1 = new DataGridViewCheckBoxCell();
            ch1 = (DataGridViewCheckBoxCell)row.Cells[0];

            if (ch1.Value == null)
                ch1.Value = false;
            switch (ch1.Value.ToString())
            {
                case "True":
                    ch1.Value = false;
                    break;
                case "False":
                    ch1.Value = true;
                    break;
            }
        }

然后,检查值的下一个按钮只是查找空值。
foreach (DataGridViewRow row in rows)
            {
                var ch1 = new DataGridViewCheckBoxCell();
                ch1 = (DataGridViewCheckBoxCell)row.Cells[0];

                if (ch1.Value == null)
                    ch1.Value = false;
                switch (ch1.Value.ToString())
                {
                    case "True":
                        ch1.Value = true;
                        break;
                    case "False":
                        ch1.Value = false;
                        break;
                }
                var val = row.Cells["EbayListingID"].Value.ToString();
                if (ch1.Value.ToString() == "true") continue;
                var listing = dsEntities.EbayListings.First(x => x.EbayListingID.ToString() == val);
                SubmitListingForReview(listing, false);
            }

你可能做错了什么,但是如果我们不知道你在做什么,我们就无法知道哪里出了问题。 - jmcilhinney
你可以设置和读取数据,就像普通的组合框一样。如果你希望我们告诉你哪里出错了,那么我们需要看到你的代码。 - deathismyfriend
尝试发布您的代码。这将帮助我们了解您已经尝试过什么和未尝试过什么。然而,我的假设是您的模型未能正确开发以存储此复选框的结果。您是否正在进行数据绑定datagridview或手动创建行? - Brett Wolfington
1个回答

0
首先,
if (ch1.Value.ToString() == "true") continue;

为什么字符串常量是"true"而不是"True"?
其次,在下一个按钮单击处理程序中,“rows”是什么?
foreach (DataGridViewRow row in rows)

我尝试了这段代码,它能正常工作:

private void button1_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                var ch1 = new DataGridViewCheckBoxCell();
                ch1 = (DataGridViewCheckBoxCell)row.Cells[0];

                if (ch1.Value == null)
                    ch1.Value = false;
                switch (ch1.Value.ToString())
                {
                    case "True":
                        ch1.Value = false;
                        break;
                    case "False":
                        ch1.Value = true;
                        break;
                }
            }
        }

private void button2_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                var ch1 = new DataGridViewCheckBoxCell();
                ch1 = (DataGridViewCheckBoxCell)row.Cells[0];

                if (ch1.Value == null)
                    ch1.Value = false;
                switch (ch1.Value.ToString())
                {
                    case "True":
                        ch1.Value = true;
                        break;
                    case "False":
                        ch1.Value = false;
                        break;
                }
                var val = row.Cells[1].Value;
                if (ch1.Value.ToString() == "True") continue;
                MessageBox.Show("1");
            }
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接