0

我有两个datagridview。具有相同的列标题但不同的单元格数据。

第一个称为grid_db 第二个称为grid_statement。

如果 grid_db 的值与 cell[j] 处的 grid_statement 的值不同,我必须突出显示单元格(红色)。我尝试了以下

int no_of_col = grid_db.Columns.Count;
int j;

for (j = 0; j < no_of_col;)
{ 
    //if statement value is null replace with ZERO
    if (grid_statement.Rows[0].Cells[j].Value != null &&
        !string.IsNullOrWhiteSpace(grid_statement.Rows[0].Cells[j].Value.ToString()))
    {
        B = grid_statement.Rows[0].Cells[j].Value.ToString();
    }


    //if db value is null replace with zero
    if (grid_db.Rows[0].Cells[j].Value != null &&
        !string.IsNullOrWhiteSpace(grid_db.Rows[0].Cells[j].Value.ToString()))
    {
         A = grid_db.Rows[0].Cells[j].Value.ToString();
    }

    if (A != B)
    {
        grid_db.Rows[0].Cells[j].Style.BackColor = Color.Red;
        grid_statement.Rows[0].Cells[j].Style.BackColor = Color.Red;

        j++;
    }
}

但它不起作用。上面的代码突出显示了两个网格的所有列。帮助 ?

4

2 回答 2

1

我试过你的代码,它对我有用,我唯一改变的是for循环在每次通过时递增,否则它很容易是无限的(它只适用于第一行,因为你的代码就是这样做的):

  public Form1()
    {
        InitializeComponent();

        grid_db.DataSource = new[]
        {
            new{
               id = 1,
               tekst="a"
                },
                new
                    {
                        id=2,
                        tekst="b"
                    }
        }.ToList();
        grid_statement.DataSource = new[]
        {
            new{
               id = 1,
               tekst="b"
                },
                new
                    {
                        id=2,
                        tekst="c"
                    }
        }.ToList();
         Load += (sender, args) =>
                    {
                        HighlightRows();
                    };
    }
    private void HighlightRows()
    {
        int no_of_col = grid_db.Columns.Count;
        int j;
        var B = "";
        var A = "";
        for (j = 0; j < no_of_col; j++)
        {
            //if statement value is null replace with ZERO
            if (grid_statement.Rows[0].Cells[j].Value != null &&
                !string.IsNullOrWhiteSpace(grid_statement.Rows[0].Cells[j].Value.ToString()))
            {
                B = grid_statement.Rows[0].Cells[j].Value.ToString();
            }
            //if db value is null replace with zero
            if (grid_db.Rows[0].Cells[j].Value != null &&
                !string.IsNullOrWhiteSpace(grid_db.Rows[0].Cells[j].Value.ToString()))
            {
                A = grid_db.Rows[0].Cells[j].Value.ToString();
            }
            if (A != B)
            {
                grid_db.Rows[0].Cells[j].Style.BackColor = Color.Red;
                grid_statement.Rows[0].Cells[j].Style.BackColor = Color.Red;

            }
        }
    }
于 2013-08-30T11:47:45.643 回答
0
var differentCells = 
       grid_db.Rows.OfType<DataGridViewRow>()
                   .SelectMany(r=>r.Cells.OfType<DataGridViewCell>())
                   .Where(c=>!grid_statement[c.ColumnIndex,c.RowIndex].Value.Equals(c.Value));
//loop through all the cells and make them red
foreach(var cell in differentCells)
  cell.Style.BackColor = Color.Red;
于 2013-08-30T11:36:44.147 回答