title: DataGrid Row Background Based On Cell Value
body: <p>I am currently working on a C# WPF datagrid. I have a DataGrid which has auto generated columns and the code connects to an SQLite Database and creates a dataset and then this dataset is set as the DataGrid ItemsSource. </p>
  
  <p>Below is the code with the XAML of the DataGrid</p>
  
  <pre><code>&lt;DataGrid AutoGenerateColumns="True"
            Margin="12,71,12,32"
            Name="tblLog"
            ColumnWidth="*"
            CanUserResizeRows="False"
            AreRowDetailsFrozen="False"
            CanUserAddRows="True"
            CanUserDeleteRows="True"
            IsReadOnly="True"
            MouseDoubleClick="tblLog_MouseDoubleClick"&gt;                
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>And below is the code to set the ItemsSource for the DataGrid</p>
  
  <pre><code>try
  {
      DataSet ds = new DataSet();
      SQLiteDataAdapter da = new SQLiteDataAdapter(query, db.conn);
      da.Fill(ds);
  
      //tblGrid.AutoGenerateColumns = true;
      tblGrid.ItemsSource = ds.Tables[0].DefaultView;                    
  }
  catch (SQLiteException ex)
  {
      MessageBox.Show("Unable to retrieve logins from database.\n\n" + ex.Message + "\n\nError Code: " + ex.ErrorCode);
  }
  </code></pre>
  
  <p>The columns that are shown in the database (auto generated) are ID, date, time, status. 
  What I need to be able to do is if the value in a row of the status column equals Error change the background colour of that row. </p>
  
  <p>I assume I need to add some sort of styling tags and DataTriggers within the DataGrid tags but not sure what I need. Anything I have tried to the code that sets the ItemsSource displays an error saying that the Source needs to be empty before adding the ItemsSource.</p>
  
  <p>Thanks for any help you can provide. </p>