复选框未在DataGridView C#中被检查

发布于 2025-02-11 06:27:50 字数 777 浏览 3 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丑丑阿 2025-02-18 06:27:50

我要说的是,如果网格dataSourcedataTable,而数据表中的CompanyLead列是的字符串类型,然后只需在下面添加代码即可在现有表中添加bool“表达式”列。

这样,您所显示的所有已发布的代码都不需要。像…

ds.Tables[0].Columns.Add("CheckBoxColumn", typeof(bool), "IIF(CompanyLead = 1, true, false)");

Is what I am getting at is that if the grids DataSource is a DataTable AND the CompanyLead column in the data table is of a string type, then simply add the code below to add a bool “Expression” column to the existing table. DataColumn.Expression Property

With this, then ALL the posted code you have shown becomes unnecessary. Something like…

ds.Tables[0].Columns.Add("CheckBoxColumn", typeof(bool), "IIF(CompanyLead = 1, true, false)");
丑丑阿 2025-02-18 06:27:50

尝试更改:

dgvCompany.Rows[i].Cells["checkBoxColumn"].Value =  true;

to:

dgvCompany.Rows[i].Cells["checkBoxColumn"].Checked =  true;

如果检查未出现在IDE中,请尝试将此单元格投入到复选框

var test = (Checkbox)dgvCompany.Rows[i].Cells["checkBoxColumn"];
test.Checked = true;

Try to change:

dgvCompany.Rows[i].Cells["checkBoxColumn"].Value =  true;

to:

dgvCompany.Rows[i].Cells["checkBoxColumn"].Checked =  true;

if the checked not appear in the IDE, try to cast this cell to a checkbox

var test = (Checkbox)dgvCompany.Rows[i].Cells["checkBoxColumn"];
test.Checked = true;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文