title: How to highlight a row and force row/cell refresh in a WPF DataGrid?
body: <p>This is my first question on SOF, so please be gentle on me :)</p>
  
  <p>I have a simple WPF DataGrid defined in XAML. The application loads "user" data (FirstName, LastName) into the grid from a text file, and after loading the entries from a text file, the user hit the submit button, I want to iterate over the items in the list and submit each row to a webservice that creates Users and then display the reply from the webservice in the grid.</p>
  
  <p>In order to keep the user informed, I want to somehow highlight the current row being submitted, and I want the message to appear immediatly after the response is received. If the message is an error, highlight the row in red, if success, highlight it in green.</p>
  
  <p>Seems simple enough...</p>
  
  <p>I have a simple GridDto class that represents the Data to map to each row. It implements the INotifyPropertyChanged interface. For simplicity sake, I define it as following:</p>
  
  <pre><code>class GridDto : INotifyPropertyChanged
  {
    public string FirstName { get; set; }
    public string LastName { get; set; }
  
    private string Reply;
    public string Reply
    {
        get
        {
          return this.message;
        }
        set
        {
            this.message = value;
            this.PropertyChanged(this, new PropertyChangedEventArgs("Reply"));
        }
     }
  </code></pre>
  
  <p>The grid accepts a list of FirstName and LastNames from a text file.</p>
  
  <p>The way I populate this grid is to build a collection of <code>ObservableCollection&lt;GridDto&gt;</code> and then I do:</p>
  
  <pre><code>resultGrid.ItemsSource = ConvertTextDtoToGridDtos(result.Elements);
  </code></pre>
  
  <p>So far so good. Now I want to submit each of the rows of my grid to a webservice that create Users. The only parameters the websrvice requires is FirstName and LastName.</p>
  
  <p>Since I am new to WPF, I have no idea on how to do that. Here is what I did so far:</p>
  
  <pre><code>for (int i = 0; i &lt; resultGrid.Items.Count - 1; i++)
  {
      var dto = (GridDto)this.resultGrid.Items[i];
      var color = Color.FromRgb(192, 192, 25);
      try{
         // Do webservice call using the dto data
      }
      catch(Exception ex){
        dto.Reply = ex.Message;
        color = Color.FromRgb(255, 0, 0);
      }
  
    DataGridRow row = (DataGridRow)resultGrid.ItemContainerGenerator.ContainerFromIndex(i);
    row.Background = new SolidColorBrush(color) { Opacity = 0.45 };
  }
  </code></pre>
  
  <p>This somewhat works, but doesn't highlight the row being treated currently.</p>
  
  <p>I tried adding the following in my loop</p>
  
  <pre><code>resultGrid.SelectedIndex = i;
  </code></pre>
  
  <p>Or...</p>
  
  <pre><code>resultGrid.SelectedItem = resultGrid.Items[i];
  resultGrid.ScrollIntoView(resultGrid.Items[i]);
  </code></pre>
  
  <p>And only the last row is highlighted at the end of the loop, when everything seems to be updated.</p>
  
  <p>The other problem with this code is that the Reply column only gets refreshed at the end, after all the rows have been submitted, so there is no realtime informing the user.</p>
  
  <p><strong>How do I highlight in a smart way the row currently treated, and most importantly, how do I update the reply cell after every reply, and before processing the next row.</strong></p>
  
  <p>Thanks for your help, and sorry for my long question.</p>
  
  <p>Edit: if there is a better way of doing this all together, I am opened for suggestions.</p>