Vlookup多个条件C#

时间:2018-09-03 18:26:16

标签: c# indexing match vlookup

我有两个datagridviews: 带有网格A和B(单元格[0],单元格1)的datagridView1:

enter image description here

datagridView2 (Cells[0], Cells[1], Cells[2]):

enter image description here

我想比较两个表之间的值,如果: -the value in datagridView1.rows[i].Cells[0].value = datagridView2.rows[j].Cells[0].value AND the value in datagridView1.rows[i].Cells[1].value = datagridView2.rows[j].Cells[1].value THEN write in datagridView1.rows[i].Cells[2].value the value from the third column in datagridView2:

enter image description here

2 个答案:

答案 0 :(得分:0)

我相信您正在寻找类似的东西。这取决于您如何填充DataGridView。也没有应该执行的错误处理。

for (var i = 0; i < dataGridView1.Rows.Count; i++)
{
    var r1 = dataGridView1.Rows[i];
    var r2 = dataGridView2.Rows[i];

    if (r1.Cells[0].Value == r2.Cells[0] && r1.Cells[1].Value == r2.Cells[1])
        r1.Cells[2].Value = r2.Cells[2].Value;
}

答案 1 :(得分:0)

这是我添加的用于匹配任何行并可以正常运行的第二个循环:

        for (var i = 0; i < src1.Rows.Count; i++)

        {
            var r1 = src1.Rows[i];

            for (var j = 1; j < src2.Rows.Count; j++)
            {
                var r2 = src2.Rows[j];

                if (r1[0].Equals(r2[0]) && r1[1].Equals(r2[1]))
                {
                    r1[2] = r2[2];
                }
            }
        }