更改类似行的行背景颜色

时间:2015-09-04 09:42:22

标签: c# winforms datagridview

我刚才问了类似的问题,但这是为了linq。我有一个列表,linq用于根据3列的记录进行分组。我已将列表绑定到DataGridView,并根据需要显示记录。

原始记录是这样的,

var list = new List<Users>()
{ 
  new Users() { FirstName = "A1", LastName= "A2", MiddleName = "A3" },      
  new Users() { FirstName = "B1", LastName= "A2", MiddleName = "B3" },
  new Users() { FirstName = "C1", LastName= "C2", MiddleName = "C3" },
  new Users() { FirstName = "A1", LastName= "A2", MiddleName = "A3" },
  new Users() { FirstName = "D1", LastName= "C2", MiddleName = "A3" },
  new Users() { FirstName = "A1", LastName= "A2", MiddleName = "B3" },
  new Users() { FirstName = "B1", LastName= "B2", MiddleName = "A3" },
};

应用linq后,

var sorted = list.OrderBy(l => l.FirstName).ThenBy(l => l.LastName).ThenBy(l => l.MiddleName).ToList();

记录按FirstName,LastName和MiddleName排序。所以所有行都是相似行的组。我希望我已经很好地解释了这一点。

现在我想使用Winform DataGridView的替代行颜色,但是对于类似的行而不是交替的行。

这里有一个类似的问题, How can we format group of data in DataGridView of Winform

但这是基于一列的分组。但我没有使用group by,并且根据相似的值将行分类为组。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

试试这个

public partial class Form1 : Form
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
    private static readonly Random _random = new Random();
    public Form1()
    {
        InitializeComponent();
        LoadDataGridView();
    }

    private void LoadDataGridView()
    {
        var list = new List<Users>()
        { 
          new Users { FirstName = "A1", LastName= "A2", MiddleName = "A3" },
          new Users { FirstName = "B1", LastName= "A2", MiddleName = "B3" },
          new Users { FirstName = "C1", LastName= "C2", MiddleName = "C3" },
          new Users { FirstName = "A1", LastName= "A2", MiddleName = "A3" },
          new Users { FirstName = "D1", LastName= "C2", MiddleName = "A3" },
          new Users { FirstName = "A1", LastName= "A2", MiddleName = "B3" },
          new Users { FirstName = "B1", LastName= "B2", MiddleName = "A3" },
          new Users { FirstName = "D1", LastName= "C2", MiddleName = "A3" },
          new Users { FirstName = "B1", LastName= "B2", MiddleName = "A3" },
          new Users { FirstName = "A1", LastName= "A2", MiddleName = "A3" }
        };
        foreach (var user in list)
        {
            var localCopy = user;
            var assignedColor = list.Where(x => x.FirstName == localCopy.FirstName && x.LastName == localCopy.LastName && x.MiddleName == localCopy.MiddleName && x.BackColor != null).Select(x => x.BackColor).FirstOrDefault();
            user.BackColor = assignedColor ?? Color.FromKnownColor(GetRandomConsoleColor());
            var index = dataGridView1.Rows.Add();
            dataGridView1.Rows[index].Cells[0].Value = user.FirstName;
            dataGridView1.Rows[index].Cells[1].Value = user.LastName;
            dataGridView1.Rows[index].Cells[2].Value = user.MiddleName;
            dataGridView1.Rows[index].DefaultCellStyle.BackColor = user.BackColor ?? Color.White;
        }
    }

    private static KnownColor GetRandomConsoleColor()
    {
        var consoleColors = Enum.GetValues(typeof(KnownColor));
        return (KnownColor)consoleColors.GetValue(_random.Next(consoleColors.Length));
    }
}

public class Users
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MiddleName { get; set; }
    public Color? BackColor { get; set; }
}

enter image description here

编辑:

对于浅色随机颜色,请替换以下代码行

user.BackColor = assignedColor ?? Color.FromKnownColor(GetRandomConsoleColor());

user.BackColor = assignedColor ?? Color.FromArgb(Random.Next(200, 255), Random.Next(150, 255), Random.Next(150, 255));

或者您可以找到更有效的方法来生成浅色随机颜色:)