0

I'm trying to change the color of my datagridrows based on the ID change. I have already bind the data to the datagridview.

I think the easiest way would be to check if the ID in Column 1 is odd or even:

even rowbackground = white odd rowbackground = brown

The Idea is to get a better overview and collect the items a bit due to background color.

Im complety new to WPF: At the moment i have just the datagrid included as follows:

<DataGrid x:Name="cusDetailGrid" Grid.Column="1" Grid.Row="2" ItemsSource="{Binding}"/>

This will be populated by a selectionstring to MySqlDB.

Any Ideas how to get this working? Thank you in advance!

  • Set [AlternatingRowBackground](https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.datagrid.alternatingrowbackground?view=net-5.0)? – Clemens Jan 11 '21 at 22:18
  • I have tried already but this is not what im looking for or i dont know how to use it how i want. Let's say my table looks like this: 1 XY (white) 1 XY (white) 1 XY (white) 2 ZB (brown) 2 ZB (brown) 3 Bla (white) 3 Bla (white) 3 Bla (white – Plumped Jan 11 '21 at 22:25

1 Answers1

0

Here looks like a good answer.

One other way you might be able to achieve this is something along the lines of creating a LoadingRow event; you could try something like this and then just adjust to wherever the ID column is located:

private void Dg_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            try
            {
                if (Convert.ToInt32(((System.Data.DataRowView)(e.Row.DataContext)).Row.ItemArray[0]) % 2 != 0)
                {
                    e.Row.Background = new SolidColorBrush(Colors.Brown);
                }
                else
                {
                    e.Row.Background = new SolidColorBrush(Colors.White);
                }
            }
            catch
            {

            }
        }