Add more than 1 DataGridCell with style to a row in wpf dgv programatically
<p>I have a class <code>Ro</code> that has 4 fields (2 names and 2 colors)</p>

<pre><code>public class Ro
    {
        public string c1 { get; set; }
        public SolidColorBrush c1Color { get; set; }
        public string c2 { get; set; }
        public SolidColorBrush c2Color { get; set; }
    }
</code></pre>

<p>I have created a <code>List of Ro objects</code></p>

<pre><code>List&lt;Ro&gt; data = new List&lt;Ro&gt;();
            data.Add(new Ro()
            {
                c1 = "7B",
                c1Color = Brushes.Green,
                c2 = "",
                c2Color = Brushes.White
            });
            data.Add(new Ro()
            {
                c1 = "Jot",
                c1Color = Brushes.Green,
                c2 = "",
                c2Color = Brushes.Black
            });
            data.Add(new Ro()
            {
                c1 = "Nav",
                c1Color = Brushes.White,
                c2 = "",
                c2Color = Brushes.Orange
            });
</code></pre>

<p>Now I want to use this List to populate a <code>wpf DataGridView</code> assigning the color to each cell depending on current field of object when looping the list </p>

<p>To do so I created a method that will create ControlTemplate for each cell:</p>

<pre><code>        public ControlTemplate CellTemplate(string text, SolidColorBrush color)
        {
            ControlTemplate template = new ControlTemplate();
            template.VisualTree = new FrameworkElementFactory(typeof(TextBlock));
            template.VisualTree.SetValue(TextBlock.TextProperty, text);
            template.VisualTree.SetValue(TextBlock.BackgroundProperty, color);
            template.VisualTree.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Center);
            template.VisualTree.SetValue(TextBlock.FontWeightProperty, FontWeights.Bold);
            if (color == Brushes.White)
                template.VisualTree.SetValue(TextBlock.ForegroundProperty, Brushes.Black);
            else
                template.VisualTree.SetValue(TextBlock.ForegroundProperty, Brushes.White);
            return template;
        }
</code></pre>

<p>and also I created a dataColumn <code>c1</code></p>

<pre><code>dataGrid1.Columns.Add(
    new DataGridTextColumn
    { Header = "c1" });
</code></pre>

<p>finally in a foreach loop I create the cells with styles</p>

<pre><code>foreach (Ro me in data)
{
    DataGridCell cell0 = new DataGridCell { Template = CellTemplate(me.c1,me.c1Color) };
    dataGrid1.Items.Add(cell0);
  }
</code></pre>

<p><a href="https://i.stack.imgur.com/A53sh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/A53sh.png" alt="enter image description here"></a></p>

<p>so far so good, however when I add the second column and try to apply the same idea like</p>

<pre><code> dataGrid1.Columns.Add(
    new DataGridTextColumn
    { Header = "c2" });

foreach (Ro me in data)
{
    DataGridCell cell0 = new DataGridCell { Template =    CellTemplate(me.c1,me.c1Color) };
    dataGrid1.Items.Add(cell0);

   DataGridCell cell1 = new DataGridCell { Template = CellTemplate(me.c2, me.c2Color) };
    dataGrid1.Items.Add(cell1);
}
</code></pre>

<p>i get excepction:</p>

<blockquote>
  <p>System.ArgumentOutOfRangeException was unhandled on
  System.Windows.Media.VisualCollection.get_Item(Int32 index)
         on System.Windows.Controls.UIElementCollection.get_Item(Int32 index)
         on System.Windows.Controls.UIElementCollection.System.Collections.IList.get_Item(Int32
  index)
         on System.Windows.Controls.DataGridCellsPanel.ArrangeOverride(Size
  arrangeSize) ...</p>
</blockquote>

<p>if I have only one column in dgv I get:
<a href="https://i.stack.imgur.com/XuKUi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XuKUi.png" alt="enter image description here"></a></p>

<p>I tried doing following, but got error, I do not know how to insert a particular cell in a particular column in dgv...</p>

<pre><code> foreach (Ro me in data)
            {
                DataGridCell cell0 = new DataGridCell { Template = CellTemplate(me.c1, me.c1Color) };
                DataGridCell cell1 = new DataGridCell { Template = CellTemplate(me.c2, me.c2Color) };
                dataGrid1.Columns.Insert(0, cell0);
                dataGrid1.Columns.Insert(1, cell0);
                //dataGrid1.Items.Add(cell0);
                //dataGrid1.Items.Add(cell1);
            }
</code></pre>

<p>How can I add cells in a row programatically?</p>