Search code examples
c#wpfdatagridwpfdatagridstyling

How to color the columns of a wpf datagrid when columns are defined and added be c# code?


To be more specific I'm using the DataGrid just as a way to show the apps operation history. The problem is that for the user's eyes, it's too difficult to recognize the columns. So I decided to color the columns one by one in looping colors eg. First white, second blue, third white and...

As I don't know WPF markup very well, I defined and applied the columns by the c# function below:

 private void generate_columns()
    {
        DataGridTextColumn c1 = new DataGridTextColumn();
        c1.Header = "Shot number";
        c1.Binding = new Binding("shotNum");
        c1.Width = 80;
        dataGrid.Columns.Add(c1);
        DataGridTextColumn c2 = new DataGridTextColumn();
        c2.Header = "Shooter name";
        c2.Width = 160;
        c2.Binding = new Binding("shooter");
        dataGrid.Columns.Add(c2);
        DataGridTextColumn c3 = new DataGridTextColumn();
        c3.Header = "shot 1";
        c3.Width = 120;
        c3.Binding = new Binding("shoot1");
        dataGrid.Columns.Add(c3);
        DataGridTextColumn c4 = new DataGridTextColumn();
        c4.Header = "shot 2";
        c4.Width = 120;
        c4.Binding = new Binding("shoot2");
        dataGrid.Columns.Add(c4);
        DataGridTextColumn c5 = new DataGridTextColumn();
        c5.Header = "shot 3";
        c5.Width = 120;
        c5.Binding = new Binding("shoot3");
        dataGrid.Columns.Add(c5);
        DataGridTextColumn c6 = new DataGridTextColumn();
        c6.Header = "Addition";
        c6.Width = 180;
        c6.Binding = new Binding("addition");
        dataGrid.Columns.Add(c6);
        DataGridTextColumn c7 = new DataGridTextColumn();
        c7.Header = "Player1 score";
        c7.Width = 160;
        c7.Binding = new Binding("scoreh");
        dataGrid.Columns.Add(c7);
        DataGridTextColumn c8 = new DataGridTextColumn();
        c8.Header = "Player2 score";
        c8.Width = 160;
        c8.Binding = new Binding("scoreo");
        dataGrid.Columns.Add(c8);
    }

in which c1,c2,...,c8 are TextColumns and dataGrid is the name of the DataGrid. Also, saying that generate_columns() function is called at the window startup.

My question is that can I, and if I could what such change shall I do to the above code so I can take control of the color and change it the way I mentioned?

Any bits of help or idea is highly regarded


Solution

  • If you really want to do it in C#, you can set the column's cell color using:

    c1.CellStyle = new Style(typeof(DataGridCell));
    c1.CellStyle.Setters.Add(new Setter(BackgroundProperty, new SolidColorBrush(Colors.LightBlue)));
    

    So, if you want to do alternating colors, a quick method would be:

    for (int i = 0; i < dataGrid.Columns.Count; i++)
    {
        var desiredColor = new SolidColorBrush(i % 2 == 0 ? Colors.White : Colors.LightBlue);
    
        dataGrid.Columns[i].CellStyle = new Style(typeof(DataGridCell));
        dataGrid.Columns[i].CellStyle.Setters.Add(new Setter(BackgroundProperty, desiredColor));
    }
    

    i % 2 == 0 ? is used to alternate the color for even and odd numbers