3

我看过一些类似这个问题的帖子,但我还没有想出我的答案,所以我想我会再次浮动它看看会发生什么......

我正在使用 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;
    }
}

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

4

1 回答 1

4

您可以通过将值设置为 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";                        
    }
}
于 2014-10-23T21:57:59.660 回答