【问题标题】:c# highlight cells in a data grid view where one specific cell is duplicate and any other cell is duplicatec#在数据网格视图中突出显示单元格,其中一个特定单元格是重复的,任何其他单元格都是重复的
【发布时间】:2018-12-11 15:08:08
【问题描述】:

您好,我有一个数据网格视图,其中填充了数据,比如姓名、年龄和爱好。

现在我希望在年龄重复并且名称或爱好都重复但年龄需要重复的字段的地方突出显示 被突出显示。

到目前为止,我已经找到了突出显示年龄相同的行并突出显示该行中的年龄列的代码:

var rows = dataGridView1.Rows.OfType<DataGridViewRow>()

var duplettes = rows.GroupBy(dgvrow => dgvrow.Cells["age"].Value.ToString())
            .Where(item => item.Count() > 1)
            .SelectMany(dgvrow => dgvrow.ToList());


    foreach (var row in duplettes)
{
            row.DefaultCellStyle.BackColor = Color.Yellow;
            row.Cells["age"].Style.ForeColor = Color.Red;
}

如果两个孩子的年龄相同,我希望通过更改前景色来突出显示他们的年龄,并且我希望通过更改背景颜色来突出显示他们所在的行。

如果两个孩子被称为 bob(或 sally 或其他什么,名字只需要相同)并且他们的年龄相同,我希望通过更改前景色来突出显示他们的名字。

现在,如果两个孩子被命名为 bob(或 sally 或其他什么,名字只需要相同),并且他们都有一个重复退出的年龄,但他们的年龄不同,我不希望他们的名字出现突出显示。

爱好也一样。

【问题讨论】:

    标签: c# datagridview duplicates highlight


    【解决方案1】:

    这是我的解决方案。我假设您希望突出显示满足逻辑条件row1.age == row2.age =&gt; (row1.name == row2.name || row1.hobby == row2.hobby) 的行。如果您寻求不同的条件,请告诉我。

    编辑:抱歉,我最初的回答错误地设置了黄色

    编辑 2:应该实际更改网格视图的更新版本

    编辑 3:使用不同的方法更新行

    编辑 4:清除数据源,然后添加包含旧值的修改列表

    public void HighlightRows()
    {
        var rows = dataGridView1.Rows.OfType<DataGridViewRow>().ToList();
    
        DataGridViewRow row = new DataGridViewRow();
    
        MatchAllRows(rows);
    
        dataGridView1.DataSource = null;
        dataGridView1.Rows.Clear();
        dataGridView1.Rows.AddRange(rows.ToArray());
    }
    
    private void MatchAllRows(List<DataGridViewRow> rows)
    {
        for (int i = 0; i < rows.Count() - 1; i++)
            for (int j = i + 1; j < rows.Count(); j++)
                if (rows[i].Cells["age"] == rows[j].Cells["age"])
                    MatchTwoRows(rows[i], rows[j]);
    }
    
    private void MatchTwoRows(DataGridViewRow row1, DataGridViewRow row2)
    {
        void Match(string key)
        {
            if (row1.Cells[key].Value == row2.Cells[key].Value)
            {
                row1.Cells[key].Style.ForeColor = Color.Red;
                row2.Cells[key].Style.ForeColor = Color.Red;
    
                row1.Cells[key].Style.BackColor = Color.Yellow;
                row2.Cells[key].Style.BackColor = Color.Yellow;
            }
        }
    
        List<string> keys = new List<string> { "name", "age", "hobby" };
    
        foreach (string key in keys)
            Match(key);
    }
    

    【讨论】:

    • 不知何故这对我不起作用,没有突出显示。为了更彻底地解释需要强调的内容,我在这里制作了这张照片:fs1.directupload.net/images/180703/b2whhuzc.png
    • 在图片中我不小心调用了datagridviewrows dataviewrows
    • @moriturus:谢谢你的照片。我认为我的应用程序逻辑正在做你想做的事,我只是在修改 ToList() 的副本。我已经用希望修复的方法更新了我的解决方案
    • 绑定列表作为数据源不太有效(datagridview 显示列表的设置,例如只读而不是值。您知道我该如何更改它吗?
    • @moriturus:我只是在查看代码并发现了那个错误。对不起,我希望我可以自己测试一下。我想我可以解决它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    相关资源
    最近更新 更多