【问题标题】:The best way to set all the datagrid cells of a row with the same value将一行的所有数据网格单元格设置为相同值的最佳方法
【发布时间】:2016-04-16 21:23:14
【问题描述】:

我想改进此代码,其目标是像 Excel 过滤器一样工作。如果所选行的所有复选框都为真,则函数(由按钮触发)将它们全部设置为假,如果它们都为假,则将它们设置为真,并且如果其中至少一个为falsetrue,那么它们都变成了真的。但是我的代码只有在它们都是truefalse 时才有效。另一个条件并不总是有效,这是因为代码的最后一部分没有抛出正确的结果。 复选框位于第 3 列,从 7 到 19 共 21 个。

public void seleciona_check()
{
  for (int i = 7; i < grid_lic.ColumnCount-1 ; i++)
  {
    for (int j = 7; j < grid_lic.ColumnCount - 1; j++)
    {
      if (grid_lic.CurrentRow.Cells[j].Value.ToString() == grid_lic.CurrentRow.Cells[i].Value.ToString())
      {
       if (grid_lic.CurrentRow.Cells[i].Value.ToString() == "True")
        {
          Convert.ToBoolean(grid_lic.CurrentRow.Cells[i].Value = false);
          Convert.ToBoolean(grid_lic.CurrentRow.Cells[3].Value = false);
        }
        else if (grid_lic.CurrentRow.Cells[i].Value.ToString() == "False")
        {
          Convert.ToBoolean(grid_lic.CurrentRow.Cells[i].Value = true);
          Convert.ToBoolean(grid_lic.CurrentRow.Cells[3].Value = true);
        }
      }
      else if (grid_lic.CurrentRow.Cells[j].Value.ToString() != grid_lic.CurrentRow.Cells[i].Value.ToString())
      {
        Convert.ToBoolean(grid_lic.CurrentRow.Cells[i].Value = true);
        Convert.ToBoolean(grid_lic.CurrentRow.Cells[j].Value = true);
        Convert.ToBoolean(grid_lic.CurrentRow.Cells[7].Value = true);
        Convert.ToBoolean(grid_lic.CurrentRow.Cells[3].Value = true);
      }
    }
  }
}

【问题讨论】:

  • 您能否分享有关此问题的更多信息。例如网格的屏幕截图可能有助于理解实际问题。
  • 我已经插入了数据网格的图像。当我选择所有复选框为真(或填充)的行并应用过滤器时,它们不会变为假。
  • 第 3 列对应于 'bloqueado' 列。我更改了它的索引,使其显示在数据网格的位置 6

标签: c# checkbox datagridview filter datagrid


【解决方案1】:

如果要处理行,则应遍历行,然后遍历行的每一列。如果要处理特定行,则必须访问当前行索引并使用它来访问细胞。

您可以从第一列的值开始,并检查它在迭代时是否发生变化。如果它发生变化,则调用一个函数来根据需要设置值,如果没有,则在最后调用该函数。

public void seleciona_check()
    {
        bool changed = false; //boolean to see if something changed 
        bool compareTo=(bool)grid_lic.CurrentRow.Cells[3].Value; // take the first value as reference point
        for (int j = 7; j < grid_lic.ColumnCount - 1; j++) // for loop to check
        {
            if ((bool)grid_lic.CurrentRow.Cells[j].Value != compareTo)
            {
                changed = true; 
                SetAllValues(changed);
                break; //no need to go on if there are true and false values
            }
        }
        if (changed == false) SetAllValues ( compareTo );
    }

    public void SetAllValues(bool toSet)
    {
        for (int j = 7; j < grid_lic.ColumnCount - 1; j++) // for loop to check
        {
            grid_lic.CurrentRow.Cells[j].Value = toSet;
        }
    }

【讨论】:

  • 我不想将列单元格的值与所选行的第 3 列的值进行比较,我只想设置全部为真,如果它们都是假的或至少一个假或真, 如果它们都为真,则将它们全部设置为假。就像在 excel 过滤器中一样。第 3 列不在循环中,因为在它和第 7 列之间存在没有复选框且未更改为 true 或 false 的单元格。它现在在可视化术语中处于第 6 位,因为我更改了 datagrid 中的索引可视化,但它的实际索引是 3。
猜你喜欢
  • 1970-01-01
  • 2014-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多