C# DatagridView复选框列始终为空

C# DatagridView复选框列始终为空,c#,datagridview,datagridviewcheckboxcell,C#,Datagridview,Datagridviewcheckboxcell,我有一个奇怪的问题。 基本上我有一个datagridview和一个按钮。当我点击这个按钮时,它会检查所有行的列1s值-复选框列。然后根据当前的情况将其设置为true/false 没关系 但是,我有另一个按钮来处理这些打勾的行。我单击它,它只会将第一行标识为被勾选。其余的现在显然是空的 那么,我如何通过编程在datagrid视图中设置复选框列的值,然后再次读取它,因为根据我的结果,我显然偏离了目标 这将设置勾选框,我可以看到它们,手动取消勾选等等 foreach (DataGridViewRow

我有一个奇怪的问题。 基本上我有一个datagridview和一个按钮。当我点击这个按钮时,它会检查所有行的列1s值-复选框列。然后根据当前的情况将其设置为true/false

没关系

但是,我有另一个按钮来处理这些打勾的行。我单击它,它只会将第一行标识为被勾选。其余的现在显然是空的

那么,我如何通过编程在datagrid视图中设置复选框列的值,然后再次读取它,因为根据我的结果,我显然偏离了目标

这将设置勾选框,我可以看到它们,手动取消勾选等等

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);
            }
首先,

if (ch1.Value.ToString() == "true") continue;
为什么字符串常量是“true”,而不是“true”

其次,在下一个按钮单击处理程序中,它是什么“行”

我尝试了以下代码,效果很好:

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

你可能做错了什么,但如果我们不知道你在做什么,我们就无法知道它出了什么问题。你设置和读取数据的方式与普通组合框相同。如果你想让我们告诉你你做错了什么,那么我们需要看到你的代码尝试发布你的代码。这将帮助我们了解您尝试过什么,没有尝试过什么。但是,我的假设是,您的模型未正确开发以存储此复选框的结果。您是在数据绑定datagridview还是手动创建行?
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");
            }
        }