Search code examples
c#wpfdatagrid

How to compare two datagrids items and change particular row background if certain column values are not equal


I've tried below but it would change whole datagrid's rows background color.

 foreach (DataRowView rowww in ResExDataGrid.Items)
  {
    foreach (DataRowView rowView in ResAcDataGrid.Items)
     {
      string textEX = rowww.Row.ItemArray[1].ToString();
      string textAC = rowww.Row.ItemArray[1].ToString();
      // if would set for your row if text mathches
      if (!(textEX == textAC) )
       {
         //changes whole data grid rows background
          ResExDataGrid.RowBackground = Brushes.Red;
       }
   }
}

this is with key value pair. This is how the program flow should be.

    foreach(KeyValuePair<string,string> dg in DataGrid.Items)
    {
      foreach(KeyValuePair<string,string> dg2 in DataGrid.Items)
      {
       foreach (DataRowView dataRow in DataGrid.Row)
       {
        for (int i = 0; i < DataGrid.Columns.Count; i++)
        {
         if(dg.Value != dg2.Value)
             dataRow.columns[i].Style.BackColor = Color.Red;
        }
       }
      }
    }

Help is appreciated.


Solution

  • for (int i = 0; i < FirstDataGridItemsSourceList.Count; i++)
    {
      if (FirstDataGridItemsSourceList[i].Data != SecondDataGridItemsSourceList[i].Data)
      {
        FirstDataGridItemsSourceList[i].ColorChange = SecondDataGridItemsSourceList[i].ColorChange = true;
      }
    }
    

    and then use a datatrigger and use binding to ColorChange property. PS- this only works if both datagrid items list contain same number of index.