C# 将一行的所有datagrid单元格设置为相同值的最佳方法

C# 将一行的所有datagrid单元格设置为相同值的最佳方法,c#,checkbox,datagridview,filter,datagrid,C#,Checkbox,Datagridview,Filter,Datagrid,我想改进这段代码,它的目标是像excel过滤器一样工作。如果所选行的所有复选框均为true,则函数(由按钮触发)将其全部设置为false;如果全部为false,则将其设置为true;如果至少有一个复选框为false或true,则其全部变为true。但我的代码只有在它们都是true或false时才起作用。另一个条件并不总是有效的,这是因为代码的最后一部分没有抛出正确的结果。 复选框位于第3列,从7到19,共21个复选框 public void seleciona_check() { for (

我想改进这段代码,它的目标是像excel过滤器一样工作。如果所选行的所有复选框均为true,则函数(由按钮触发)将其全部设置为false;如果全部为false,则将其设置为true;如果至少有一个复选框为
false
true
,则其全部变为true。但我的代码只有在它们都是
true
false
时才起作用。另一个条件并不总是有效的,这是因为代码的最后一部分没有抛出正确的结果。 复选框位于第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);
      }
    }
  }
}
public void seleciona_check()
{
对于(int i=7;i
如果要处理行,则应先遍历行,然后遍历行的每一列。如果要处理特定行,则必须访问当前行索引并使用它访问单元格

您可以从第一列的值开始,并检查它是否在迭代时发生更改。如果更改,则调用函数以根据需要设置值,如果未更改,则在结束时调用该函数

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;
        }
    }
public void seleciona_check()
{
bool changed=false;//布尔值以查看是否有更改
bool compareTo=(bool)grid_lic.CurrentRow.Cells[3].Value;//将第一个值作为参考点
for(int j=7;j
请您分享有关此问题的更多信息。e、 网格的屏幕截图可能有助于理解实际问题。我已经插入了datagrid的图像。当我选择所有复选框均为true(或已填充)的行并应用筛选器时,它们不会变为false。第3列对应于“bloqueado”列。我更改了它的索引,使其显示在DataGrid的位置6。我不想将列单元格的值与所选行的第3列的值进行比较,我只想在所有列都为false或至少一个false或true时将其设置为all true,如果所有列都为true,则将其设置为all false,就像在excel筛选器中一样。第3列超出了周期,因为在第3列和第7列之间有一些单元格没有复选框,不能更改为true或false。它现在在可视化术语中处于第6位,因为我在datagrid中更改了索引可视化,但它的实际索引是3。