复选框未在DataGridView C#中被检查
我在DataGridView中添加了复选框列,我希望检查复选框,如果我的列值之一== 1否则请勿选中复选框 我已经写了以下内容,其中的内容在datagridview中加载,该方法在表单加载事件中被调用 但是复选框未显示该方法在第二次被调用第二次起作用的方法时未显示检查的检查
DataGridViewCheckBoxColumn chkboxcolumn = new DataGridViewCheckBoxColumn();
chkboxcolumn.HeaderText = "";
chkboxcolumn.Width = 30;
chkboxcolumn.Name = "checkBoxColumn";
if (!dgvCompany.Columns.Contains(chkboxcolumn.Name))
{
dgvCompany.Columns.Insert(0, chkboxcolumn);
}
for (int i = 0; i < dgvCompany.Rows.Count; i++)
{
if (Convert.ToString (dgvCompany.Rows[i].Cells["CompanyLead"].Value) == "1")
{
dgvCompany.Rows[i].Cells["checkBoxColumn"].Value = true;
}
}
I have added checkbox column in datagridview and i want that checkboxes checked if one of my column value == 1 otherwise the checkbox should be unchecked
i have written following code where content loads in datagridview and that method is called at form load event
but checkboxes not showing checked when first time that method is calling when method gets called second time that is working correct
DataGridViewCheckBoxColumn chkboxcolumn = new DataGridViewCheckBoxColumn();
chkboxcolumn.HeaderText = "";
chkboxcolumn.Width = 30;
chkboxcolumn.Name = "checkBoxColumn";
if (!dgvCompany.Columns.Contains(chkboxcolumn.Name))
{
dgvCompany.Columns.Insert(0, chkboxcolumn);
}
for (int i = 0; i < dgvCompany.Rows.Count; i++)
{
if (Convert.ToString (dgvCompany.Rows[i].Cells["CompanyLead"].Value) == "1")
{
dgvCompany.Rows[i].Cells["checkBoxColumn"].Value = true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我要说的是,如果网格
dataSource
是dataTable
,而数据表中的CompanyLead
列是的字符串
类型,然后只需在下面添加代码即可在现有表中添加bool
“表达式”列。这样,您所显示的所有已发布的代码都不需要。像…
Is what I am getting at is that if the grids
DataSource
is aDataTable
AND theCompanyLead
column in the data table is of astring
type, then simply add the code below to add abool
“Expression” column to the existing table. DataColumn.Expression PropertyWith this, then ALL the posted code you have shown becomes unnecessary. Something like…
尝试更改:
to:
如果检查未出现在IDE中,请尝试将此单元格投入到复选框
Try to change:
to:
if the checked not appear in the IDE, try to cast this cell to a checkbox