0
votes

I am comparing row value from WPF Datagrid row item to database items, after finding a match I need to change the color of particular row in Datagrid. Upto finding match is working fine

  foreach (System.Data.DataRowView rowview in dataGrid1.Items )
  {
    var Srow = dataGrid1.ItemContainerGenerator.ContainerFromItem(dataGrid1.SelectedItem) as DataGridRow;
    Srow.Background = Brushes.LightGreen;
  }

giving error (Null reference unhandled.) I tried all option keeping index, rowview.Row["Name"].....

Please any suggestion in welcome.

1
you should change the selectedItemTemplate instead of changing the background manually - Domysee

1 Answers

0
votes

Hi I will suggest you to implement the LoadingRow Event, as changing your rows style will be more easy:

  //The event: 
   dataGrid1.LoadingRow += new EventHandler<DataGridRowEventArgs>(dataGrid1_LoadingRow);

Now the function that handle the event

  //Here come the function
   private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
   {
     //your test here to see if they match...
       DataRowView row = (DataRowView)e.Row.Item;
       String Content = row.Row[i].ToString();
       if(Content = //Did it matche something ?)
       { 
         //If yes:
          e.Row.Background = new SolidColorBrush(Color.FromArgb(255, 217, 77, 77));
         //or 
         e.Row.Background = Brushes.LightGreen;
       }
   } 

That's it, let me know if it workes for you.