title: How to merge multiple columns values into one column? Asp.net Gridview C#
body: <p>First of I don't know if this is possible, the correct way to go or even will work, but I hope you guys can help me out, I will try to explain:</p>
  
  <p>I have a GridView Control on the ASPX page:</p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" OnRowDataBound="GridView1_RowDataBound" GridLines="None" CssClass="table table-striped" /&gt;
  </code></pre>
  
  <p>I created a DataTable in the code-behind which holds the following data and have bind it to the Gridview control:</p>
  
  <pre><code>-----------------------------------------
  |       | Name1 | Name2 | Name3 | Name4 |
  -----------------------------------------
  | Row1  | 1     | 1     | 1     | 1b    |
  | Row2  | 1a    | 2b    | 2b    | 4b    |
  | Row3  | 2a    | 2c    | 2a    | 2a    |
  | Row4  | 1d    | 1d    | 1d    | 4d    |
  | Row5  | 1e    | 1e    | 1e    | 1e    |
  | Row6  | 1f    | 2f    | 3f    | 4f    |
  -----------------------------------------
  </code></pre>
  
  <p>Now I would like matching column values to be merged and add the appropriate colspan. I have added to the GridView control a OnRowDataBound as following:</p>
  
  <pre><code>protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
          if (e.Row.RowIndex &gt;= 0)
          {
              int colSpanValue = 2;
              for (int i = 0; i &lt; e.Row.Cells.Count; i++)
              {
                  if (i+1 &lt; e.Row.Cells.Count) 
                  {
                      if (e.Row.Cells[i].Text == e.Row.Cells[i + 1].Text)
                      {
                          e.Row.Cells[i].BackColor = Color.Beige;
                          e.Row.Cells[i].ColumnSpan = colSpanValue;
                          e.Row.Cells[i].HorizontalAlign = HorizontalAlign.Center;
                          e.Row.Cells[i + 1].Visible = false;
                          colSpanValue++;
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>so the above data would like this, something like this.</p>
  
  <pre><code>-----------------------------------------
  |       | Name1 | Name2 | Name3 | Name4 |
  -----------------------------------------
  | Row1  |       1       | 1b            | &lt;!-- problem
  | Row2  | 1a    |      2b       | 4b    |
  | Row3  | 2a    | 2c    |      2a       | &lt;!-- problem
  | Row4  |           1d          | 4d    |
  | Row5  |              1e               |
  | Row6  | 1f    | 2f    | 3f    | 4f    |
  -----------------------------------------
  </code></pre>
  
  <p>Currently I managed to get this, see screenshot
  <a href="https://i.stack.imgur.com/KLorT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KLorT.png" alt="enter image description here"></a></p>
  
  <p>However this is not really what i would like to see, but would expect as the OnRowDataBound code block is probably not done the right.</p>
  
  <p>So my question is:</p>
  
  <ul>
  <li>How could I merge all equal columns in a row and add colspan to them?</li>
  <li>Now for the tricky bit, would I be able to sort the columns so, that matching column cells are displayed properly? (not sure about this)</li>
  </ul>
  
  <p>So the ideal result would be like this:</p>
  
  <pre><code>-----------------------------------------
  |       | Name1 | Name2 | Name3 | Name4 |
  -----------------------------------------
  | Row1  |           1           | 1b    | &lt;!-- problem
  | Row2  | 1a    |      2b       | 4b    |
  | Row3  | 2a    | 2c    |      2a       | &lt;!-- problem
  | Row4  |           1d          | 4d    |
  | Row5  |              1e               |
  | Row6  | 1f    | 2f    | 3f    | 4f    |
  -----------------------------------------
  </code></pre>
  
  <p><strong>UPDATED INFORMATION</strong></p>
  
  <p>After updating the code according to the answers provided by <strong>ConnorsFan</strong> and <strong>fnostro</strong>, both answers are correct and work, thank you for the help. 
  I choose for ConnorsFan's approach, as i'm still learning. </p>
  
  <p>The collspan's are now correct, see screenshot below:</p>
  
  <p><a href="https://i.stack.imgur.com/brTVa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/brTVa.png" alt="enter image description here"></a></p>
  
  <p>I will try to <strong>fnostro's</strong> advise to manage the sorting part via the DataTable and rebind the data to the GridView1 and keep you posted. once again thank you for your answers.</p>