ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Convert dynamic DataTable Columns to Rows and Rows to Columns using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="nauna"> nauna</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:GridView ID=&#34;GridView1&#34; CssClass=&#34;Grid&#34; HeaderStyle-CssClass=&#34;header&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;true&#34;&gt; &lt;/asp:GridView&gt; &lt;br /&gt; &lt;asp:Button ID=&#34;btnConvert1&#34; runat=&#34;server&#34; Text=&#34;Convert Rows to Columns&#34; OnClick=&#34;Convert&#34; CommandArgument=&#34;1&#34; /&gt; &lt;asp:Button ID=&#34;btnConvert2&#34; runat=&#34;server&#34; Text=&#34;Convert Columns to Rows&#34; OnClick=&#34;Convert&#34; CommandArgument=&#34;2&#34; Visible=&#34;false&#34; /&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataTable dt = GetDataBaseRecord(); ViewState[&#34;dt&#34;] = dt; BindGrid(dt, false); } } private DataTable GetDataBaseRecord() { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn(&#34;username&#34;), new DataColumn(&#34;column&#34;) }); dt.Rows.Add(&#34;A&#34;, &#34;a&#34;); dt.Rows.Add(&#34;A&#34;, &#34;b&#34;); dt.Rows.Add(&#34;A&#34;, &#34;c&#34;); dt.Rows.Add(&#34;B&#34;, &#34;a&#34;); dt.Rows.Add(&#34;B&#34;, &#34;b&#34;); dt.Rows.Add(&#34;B&#34;, &#34;c&#34;); dt.Rows.Add(&#34;C&#34;, &#34;a&#34;); dt.Rows.Add(&#34;C&#34;, &#34;b&#34;); return dt; } private void BindGrid(DataTable dt, bool rotate) { GridView1.ShowHeader = !rotate; GridView1.DataSource = dt; GridView1.DataBind(); if (rotate) { foreach (GridViewRow row in GridView1.Rows) { row.Cells[0].CssClass = &#34;header&#34;; } } } protected void Convert(object sender, EventArgs e) { DataTable dt = (DataTable)ViewState[&#34;dt&#34;]; if ((sender as Button).CommandArgument == &#34;1&#34;) { btnConvert1.Visible = false; btnConvert2.Visible = true; DataTable dt2 = new DataTable(); DataTable dtgroup = dt.AsEnumerable() .GroupBy(r =&gt; new { Col1 = r[&#34;username&#34;], Col2 = r[&#34;username&#34;] }) .Select(g =&gt; g.OrderBy(r =&gt; r[&#34;column&#34;]).First()) .CopyToDataTable(); for (int i = 0; i &lt; dtgroup.Rows.Count + 1; i++) { dt2.Columns.Add(); } for (int i = 0; i &lt; dtgroup.Columns.Count - 1; i++) { for (int j = 0; j &lt; dtgroup.Rows.Count + 1; j++) { dt2.Rows.Add(); if (j &lt; dtgroup.Rows.Count) { if (j == 0) { dt2.Rows[i][j] = dtgroup.Columns[&#34;username&#34;].ColumnName; dt2.Rows[i][j + 1] = dtgroup.Columns[&#34;column&#34;].ColumnName+&#34; &#34; + (j + 1); } else { dt2.Rows[i][j + 1] = dtgroup.Columns[&#34;column&#34;].ColumnName + &#34; &#34; + (j + 1); } } } } for (int i = 0; i &lt; dtgroup.Rows.Count; i++) { dt2.Rows[i + 1][0] = dtgroup.Rows[i][&#34;username&#34;]; } for (int i = 0; i &lt; dtgroup.Rows.Count; i++) { DataRow[] rows = dt.Select(&#34;username=&#39;&#34; + dtgroup.Rows[i][&#34;username&#34;] + &#34;&#39;&#34;); for (int j = 0; j &lt; rows.Length; j++) { dt2.Rows[i + 1][j + 1] = rows[j][1]; } } BindGrid(dt2, true); } else { btnConvert1.Visible = true; btnConvert2.Visible = false; BindGrid(dt, false); } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not IsPostBack Then Dim dt As DataTable = GetDataBaseRecord() ViewState(&#34;dt&#34;) = dt BindGrid(dt, False) End If End Sub Private Function GetDataBaseRecord() As DataTable Dim dt As DataTable = New DataTable() dt.Columns.AddRange(New DataColumn() {New DataColumn(&#34;username&#34;), New DataColumn(&#34;column&#34;)}) dt.Rows.Add(&#34;A&#34;, &#34;a&#34;) dt.Rows.Add(&#34;A&#34;, &#34;b&#34;) dt.Rows.Add(&#34;A&#34;, &#34;c&#34;) dt.Rows.Add(&#34;B&#34;, &#34;a&#34;) dt.Rows.Add(&#34;B&#34;, &#34;b&#34;) dt.Rows.Add(&#34;B&#34;, &#34;c&#34;) dt.Rows.Add(&#34;C&#34;, &#34;a&#34;) dt.Rows.Add(&#34;C&#34;, &#34;b&#34;) Return dt End Function Private Sub BindGrid(ByVal dt As DataTable, ByVal rotate As Boolean) GridView1.ShowHeader = Not rotate GridView1.DataSource = dt GridView1.DataBind() If rotate Then For Each row As GridViewRow In GridView1.Rows row.Cells(0).CssClass = &#34;header&#34; Next End If End Sub Protected Sub Convert(ByVal sender As Object, ByVal e As EventArgs) Dim dt As DataTable = CType(ViewState(&#34;dt&#34;), DataTable) If (TryCast(sender, Button)).CommandArgument = &#34;1&#34; Then btnConvert1.Visible = False btnConvert2.Visible = True Dim dt2 As DataTable = New DataTable() Dim dtgroup As DataTable = dt.AsEnumerable().GroupBy(Function(r) New With {Key .Col1 = r(&#34;username&#34;), Key .Col2 = r(&#34;username&#34;)}).[Select](Function(g) g.OrderBy(Function(r) r(&#34;column&#34;)).First()).CopyToDataTable() For i As Integer = 0 To dtgroup.Rows.Count + 1 - 1 dt2.Columns.Add() Next For i As Integer = 0 To dtgroup.Columns.Count - 1 - 1 For j As Integer = 0 To dtgroup.Rows.Count + 1 - 1 dt2.Rows.Add() If j &lt; dtgroup.Rows.Count Then If j = 0 Then dt2.Rows(i)(j) = dtgroup.Columns(&#34;username&#34;).ColumnName dt2.Rows(i)(j + 1) = dtgroup.Columns(&#34;column&#34;).ColumnName &amp; &#34; &#34; + (j + 1) Else dt2.Rows(i)(j + 1) = dtgroup.Columns(&#34;column&#34;).ColumnName &amp; &#34; &#34; + (j + 1) End If End If Next Next For i As Integer = 0 To dtgroup.Rows.Count - 1 dt2.Rows(i + 1)(0) = dtgroup.Rows(i)(&#34;username&#34;) Next For i As Integer = 0 To dtgroup.Rows.Count - 1 Dim rows As DataRow() = dt.[Select](&#34;username=&#39;&#34; &amp; dtgroup.Rows(i)(&#34;username&#34;) &amp; &#34;&#39;&#34;) For j As Integer = 0 To rows.Length - 1 dt2.Rows(i + 1)(j + 1) = rows(j)(1) Next Next BindGrid(dt2, True) Else btnConvert1.Visible = True btnConvert2.Visible = False BindGrid(dt, False) End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/Kz8VreD.gif" alt="" width="250" height="397" /></p>https://www.aspforums.net:443/Threads/191782/Convert-dynamic-DataTable-Columns-to-Rows-and-Rows-to-Columns-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/191782/Convert-dynamic-DataTable-Columns-to-Rows-and-Rows-to-Columns-using-C-and-VBNet-in-ASPNet/Fri, 09 Aug 2019 07:59:01 GMT