如何在 C# 中比较 datagridview1 的 datagridview 单元格与 datagridview2 的另一个 datagridview 单元格

How can I compare datagridview cell of datagridview1 with another datagridview cell of datagridview2 in C#

[if Name(cell data) for datagridview1] == Name(cell data) for datagridview2 background color should be in red color.

我认为这应该是您正在寻找的方法。 'might' 有一些语法错误,因为我已经把它写出来了。但我希望我已经帮助你进一步解决这个问题。

不要忘记在 datagridview1 本身中定义事件。 (在 datagridview1 > 属性 > 事件下)

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
          string dgv1value = dataGridView1.Rows[e.Rowindex].Cells[e.Columnindex].Value.ToString();
          foreach (DataGridViewRow row in datagridview2.rows)
          {
               if (row.Cells[0].Value.ToString() == dgv1value)
               {
                    //Continue your code here
               }
          }
    }

如果还没有解决,请在评论中告诉我。

public static void CompareDataGridColumnForward(DataGridView dgv1, DataGridView dgv2)
    {
        try
        {
            for (int i = dgv1.RowCount - 1; i >= 0; i--)
            {
                for (int j = 0; j < dgv2.RowCount - 1; j++)
                {
                    string dgv1value = dgv1.Rows[i].Cells[0].Value.ToString();
                    foreach (DataGridViewRow row in dgv2.Rows)
                    {
                        if (row.Cells[0].Value.ToString() == dgv1value)
                        {
                            row.DefaultCellStyle.BackColor = Color.Red;
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            Debug.WriteLine(e);
        }
    }

    public static void CompareDataGridColumnReverse(DataGridView dgv1, DataGridView dgv2)
    {
        try
        {
            for (int i = dgv2.RowCount - 1; i >= 0; i--)
            {
                for (int j = 0; j < dgv2.RowCount - 1; j++)
                {
                    string dgv2value = dgv2.Rows[i].Cells[0].Value.ToString();
                    foreach (DataGridViewRow row in dgv1.Rows)
                    {
                        if (row.Cells[0].Value.ToString() == dgv2value)
                        {
                            row.DefaultCellStyle.BackColor = Color.Red;
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            Debug.WriteLine(e);
        }
    }