DatagridView复选框列始终为null
本文关键字:null 复选框 DatagridView | 更新日期: 2023-09-27 18:25:26
我有一个奇怪的问题。基本上我有一个数据网格视图和一个按钮。当我点击这个按钮时,它会检查所有行的第1s列值——复选框列。然后根据当前情况将其设置为真/假
没关系。
但是,我有另一个按钮来处理这些被勾选的行。我点击它,它只会将第一行标识为被勾选。剩下的现在显然是空的。。?
那么,我如何在数据网格视图中编程设置复选框列的值,然后再次读取它,因为根据我的结果,我显然偏离了目标。
这设置了勾选框,我可以看到它们,手动取消勾选等等。
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"?
第二,在下一个按钮单击处理程序中,什么是"行"?
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");
}
}