Problems changing line color of a Datagrid in C# Wpf

Asked

Viewed 76 times

0

I am having problems changing the color of the Datagrid lines, I use the following function

int i = 0;
private void gvDados_LoadingRow(object sender, DataGridRowEventArgs e)
{
   DataGridRow rowContext = e.Row;
   if (rowContext != null)
   {
      string Situacao = dt.Rows[i]["Situacao"].ToString();
      if (Situacao.Equals("V"))
      {
         SolidColorBrush brush = new SolidColorBrush(Color.FromArgb(100, 255, 104,0));
         rowContext.Background = brush;
      }
      else
      {
         SolidColorBrush brush = new SolidColorBrush(Color.FromArgb(100, 255, 232,0));
         rowContext.Background = brush;
      }

      i++;
   }
}

So far so good, I can adjust the colors according to what I want, the problem in question is when I use the Scroll Bar Horizontal to scroll down the logs or go up, so all the colors are unknowingly appearing randomly. How can I solve this problem so things don’t keep changing when scrolling ?

  • Change the event that does this. LoadingRow will be fired whenever the line is to be shown. It is best to do after you have populated the grid.

  • Got it, which event would you indicate me? I only got access to Datagridrow rowContext = and.Row; of each line using Loadingrow

  • But you don’t need to have access to it. What’s going to happen at this event is that e.Row will be every line of the grid every time you pass by. You can swap this idea for a loop that goes through all the grid lines.

  • I would show you some code, but for that I will need to open my visual studio, create a WPF project and test the grid.

  • So at first my thought was to create a method that could read a grid line by line and change the color, but I couldn’t and the only way was this. I’m gonna do a little research to try and do it that way

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.