title: Set datagrid row background color WPF - Loop
body: <p>I have one <code>Task</code> which go throw <code>DataGridRow</code> and do some task. When he finish it set <code>background color</code> for that row. I added cancel <code>button</code> to stop task, and continue <code>button</code> to continue where it finished last time. All work perfect except changing background color for row. </p>
  
  <p>This is XAML code, i'm new to WPF so it's not too big for <code>DataGrid</code></p>
  
  <pre><code>&lt;DataGrid 
       Name="dataGridViewMyGroups" 
       Grid.Row="0" ColumnWidth="*" 
       VerticalScrollBarVisibility="Auto" 
       IsReadOnly="True" 
       SelectionUnit="FullRow" 
       SelectionMode="Single"    
       MouseDoubleClick="dataGridViewMyGroups_MouseDoubleClick"&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>Here is a C# code for changing background color.</p>
  
  <pre><code>DataGridRow rowColor = (DataGridRow)dataGridViewMyGroups.ItemContainerGenerator
                      .ContainerFromIndex(number);                                  
  rowColor.Background = new SolidColorBrush(System.Windows.Media.Color.FromRgb(223, 227, 238)); 
  </code></pre>
  
  <p>This code work when I click on start <code>Button</code> and it change <code>Background</code> color for each <code>Row</code>. Problem is when I click cancel <code>Button</code> and then click on continue <code>Button</code>, then I got <code>NullReferenceException</code>. Continue button only check last ID in <code>DataBase Table</code>.</p>
  
  <pre><code>int number=0;
  foreach (GroupsInList group in dataGridViewMyGroups.Items)
  {
     if (fbgroupID != null &amp;&amp; check==true)
     {
         number++;
         if (fbgroupID != groupLink)
         {
           continue;
         }
         check = false;
         continue;
     }
  
       //Do something and change background (code above).
       number++;
  }
  </code></pre>
  
  <p>Code for continue <code>Button</code> work except changing row's background.</p>
  
  <p><strong>UPDATE</strong>: 
  Code for Cancel <code>Button</code>:</p>
  
  <pre><code> if (MessageBox.Show("Do you want to stop posting?", "Confirmation", 
   MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
          {
              tokenSource.Cancel(); 
          }
  </code></pre>
  
  <p>Code for Continue <code>Button</code>:</p>
  
  <pre><code>        int postID;
          string fbGroupID;
          int listID;
  
          using (OleDbConnection connection = new OleDbConnection(conn))
          {
  
             //code for getting value from `DB Table`
  
              postID = list[0].PostID;
              fbGroupID = list[0].FBGroupID;
              listID = list[0].ListForGroupID;
          }
  
          cmbSelectList.SelectedValue = listID;
          cmbSavedPosts.SelectedValue = postID;
          loadGroupsInList(); //Maybe this is problem because here I update(reload) DataGrid again.
  
          tokenSource = new CancellationTokenSource();
  
          try
          {                              
              await TaskPostToGroup(tokenSource.Token, fbGroupID, true);
          }
          catch (OperationCanceledException ex)
          {
              System.Windows.MessageBox.Show(ex.Message, "CANCELED", MessageBoxButton.OK, MessageBoxImage.Stop);
          }
          catch (NullReferenceException)
          {
              //I don't have solution for changing background color for each row when continue button is clicked
          }
          catch (Exception ex)
          {
              System.Windows.MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace, "Exception", MessageBoxButton.OK, MessageBoxImage.Error);
          }
  </code></pre>