c#更改datagridview上重复行的颜色

时间:2017-07-05 02:44:30

标签: c# datagridview

Photo of datagridview

我在上面的图片上有这个datagridview,并且“Joe”名称是重复的 如何通过c#突出显示或将颜色更改为红色以获取重复值,例如“Joe”? 感谢:)

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

    //using System.Linq;
    private void button1_Click(object sender, EventArgs e)
    {
        var rows = dataGridView1.Rows.OfType<DataGridViewRow>()
            .Reverse().Skip(1);//ignore the last empty line
        var dupRos = rows.GroupBy(r => r.Cells["name"].Value.ToString())
        .Where(g => g.Count() > 1)
        .SelectMany(r => r.ToList());
        foreach (var r in dupRos)
            r.DefaultCellStyle.BackColor = Color.Red;
        foreach (var r in rows.Except(dupRos))
            r.DefaultCellStyle.BackColor = Color.White;
    }