Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式选中DataGridView中的复选框_C#_.net_Datagridviewcheckboxcell - Fatal编程技术网

C# 以编程方式选中DataGridView中的复选框

C# 以编程方式选中DataGridView中的复选框,c#,.net,datagridviewcheckboxcell,C#,.net,Datagridviewcheckboxcell,我已经看到了一些类似于这个问题的帖子,但我还没有给出我的答案,所以,我想我会再次浮动它,看看会出现什么 我正在使用ExcelDNA使用C#.NET将API与Excel集成。我有一个DataGridView,我想检查列表中已经存在的项目 以下代码在绑定到按钮单击事件时有效,但在方法中调用代码时无效 private void SelectAllItems() { foreach (DataGridViewRow row in itemsBySupplier.Rows) {

我已经看到了一些类似于这个问题的帖子,但我还没有给出我的答案,所以,我想我会再次浮动它,看看会出现什么

我正在使用ExcelDNA使用C#.NET将API与Excel集成。我有一个DataGridView,我想检查列表中已经存在的项目

以下代码在绑定到按钮单击事件时有效,但在方法中调用代码时无效

private void SelectAllItems()
{
    foreach (DataGridViewRow row in itemsBySupplier.Rows)
    {
        DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell)row.Cells["selectItem"];
        check.Value = check.TrueValue;
    }
}
我在其他地方也遇到了同样的问题:

foreach (DataGridViewRow row in itemsBySupplier.Rows)
{
    DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell)row.Cells["selectItem"];

    string hid = row.Cells["Hid"].Value.ToString();
    if (Ws.ToCreate[_supplier].Count(i => i.Hid == hid) > 0)
    {
        check.Value = check.TrueValue;
    }
}

我已经研究了几个小时,结果完全是空的。任何帮助都将不胜感激

您可以通过将该值设置为true来完成此操作。您不需要强制转换为复选框单元格

    private void SelectAllItems()
    {
        foreach (DataGridViewRow row in itemsBySupplier.Rows)
        {
            // This will check the cell.
            row.Cells["selectItem"].Value = "true";
        }
    }

foreach (DataGridViewRow row in itemsBySupplier.Rows)
{
    string hid = row.Cells["Hid"].Value.ToString();

    if (Ws.ToCreate[_supplier].Count(i => i.Hid == hid) > 0)
    {
        row.Cells["selectItem"].Value = "true";                        
    }
}

您可以通过简单地将值设置为true或false来实现这一点。DataGridView.Rows[0]。单元格[0]。值=true;该示例将检查单元格。是否可以发布代码/运行该单元格的事件,以查找您声明在放入事件时不起作用的代码。。可能您访问的内容不正确