检查2 datagridview中是否有重复的值

时间:2019-04-20 10:10:05

标签: c#

我正在尝试将行和列与2个datagridview进行比较。 (DGV1)中的第一列s1已找到s1(DGV2)中的重复值。 (DGV1)中的第二列s2与(DGV2)中的第二列s2不匹配。代码有什么问题?

for (int i = 0; i < dataGridView1.RowCount ; i++)
 {
   for (int j = 0; j < dataGridView2.RowCount; j++)
       {
         if ( dataGridView1.Rows[i].Cells[0].Value.ToString() == 
              dataGridView2.Rows[j].Cells[0].Value.ToString())
            { 
               dataGridView1.Rows[i].Cells[0].Style.BackColor = 
               Color.Yellow;
               dataGridView2.Rows[j].Cells[0].Style.BackColor = 
               Color.YellowGreen;
            }
       }     
  }

enter image description here

3 个答案:

答案 0 :(得分:1)

 for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                for (int j = 0; j < dataGridView2.Rows.Count; j++)
                {
                    for (int k = 0; k < dataGridView1.ColumnCount-1; k++)
                    {
                        for (int m = 0; m < dataGridView2.ColumnCount-1; m++)
                        {
                            if (dataGridView1.Rows[i].Cells[k].Value.ToString() == dataGridView2.Rows[j].Cells[m].Value.ToString())
                            {
                                dataGridView1.Rows[i].Cells[k].Style.BackColor = Color.Yellow;
                                dataGridView2.Rows[j].Cells[m].Style.BackColor = Color.YellowGreen;

                            }
                        }
                    }
                }
            }

谢谢你的来信,马田。但是问题仍然存在。第一行被读取。第一列s1(DGV1)ist与(DGV2)中的第一列s1不匹配。请参见下图。 enter image description here

答案 1 :(得分:1)

尝试

for (int i = 0; i < dataGridView1.RowCount ; i++)
 {
   for (int j = 0; j < dataGridView2.RowCount; j++)
       {
         if ( (dataGridView1.Rows[i].Cells[0].Value.ToString() == 
               dataGridView2.Rows[j].Cells[0].Value.ToString()) &&
              (dataGridView1.Rows[i].Cells[1].Value.ToString() == 
               dataGridView2.Rows[j].Cells[1].Value.ToString()) )

            { 
               dataGridView1.Rows[i].Cells[0].Style.BackColor = 
               Color.Yellow;
               dataGridView1.Rows[i].Cells[1].Style.BackColor = 
               Color.Yellow;
               dataGridView2.Rows[j].Cells[0].Style.BackColor = 
               Color.YellowGreen;
               dataGridView2.Rows[j].Cells[1].Style.BackColor = 
               Color.YellowGreen;
            }
       }     
  }

答案 2 :(得分:1)

foreach (DataGridViewRow row1 in table1.Rows) //LOOP ROWS TABLE 1
{
    foreach (DataGridViewCell cell1 in row1.Cells) //LOOP COLUMNS TABLE 1
    {
        foreach (DataGridViewRow row2 in table2.Rows) //LOOP ROWS TABLE 2
        {
            foreach (DataGridViewCell cell2 in row2.Cells) //LOOP COLUMNS TABLE 2
            {
                if (cell1.Value != null && cell2.Value != null&& cell2.Value.ToString() == cell1.Value.ToString())
                {
                    cell1.Style.BackColor = Color.Yellow;
                    cell2.Style.BackColor = Color.YellowGreen;
                }
            }
        }
    }
}

嘿Marcel16,这应该可以解决您的问题:

enter image description here