Convert Gridview rows to column

Let's begin by adding 2 GridViews to a page and a button. Now we need to create a Datatable. For simplicity, I am adding data to Datatable in Gridbind method and populating the Gridview. With that data manually you can fill Datatable with database. Store this Datatable in ViewState. Now we will call this method on page load.

For converting Gridview row to column I have created a method Transpose. Now we will do the following in transpose method.

  1. We need to create a new Datatable newdt. Retrieve the old Datatable from ViewState .The number of columns in new Datatable newdt will be equal to the number of rows in dt.

  2. Add columns to the new Datatable newdt (Column Name is passed as blank ).

  3. Create a datarow dr. Iterate through all columns and add for each column value get all the values of the rows in old Datatable dt and store it in dr.

  4. Add Datarow dr to newdt. Now add click event for the button and call the transpose method.
Here is the code:
  1. < style type = "text/css" > .header {  
  2.     padding: 5px;  
  3.     background - color: #7ac0da; font-weight:bold;  
  4. }  
  5. .header content-wrapper table tr td {  
  6. border:dotted 1px solid !important;  
  7. }  
  8. </style>  
  9. <br />  
  10. <div style= "text-align: center;" > < asp: Gridview alternatingrowstyle - backcolor = "#ccffff"  
  11.     bordercolor = "Gray"  
  12.     gridlines = "Both"  
  13.     headerstyle - backcolor = "#7ac0da"  
  14.     headerstyle - bordercolor = "Gray"  
  15.     headerstyle - cssclass = "header content-wrapper"  
  16.     id = "grdNormal"  
  17.     runat = "server" > < /asp:Gridview >  
  18. </div > < div style = "text-align: center;" > < asp: linkbutton id = "lnkConvert"  
  19.     onclick = "lnkConvert_Click"  
  20.     runat = "server" > < img src = "Images/down.png"  
  21.     style = "width: 50px;" / > < /asp:linkbutton>  
  22. </div > < div style = "text-align: center;" > < asp: Gridview bordercolor = "Gray"  
  23.     gridlines = "Both"  
  24.     headerstyle - bordercolor = "Gray"  
  25.     id = "grdTranspose"  
  26.     onrowcreated = "grdTranspose_RowCreated"  
  27.     runat = "server"  
  28.     showheader = "false" > < /asp:Gridview >  
  29. </div > < /div>  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. namespace GridTranspose {  
  10.     public partial class _Default: Page {  
  11.         protected void Page_Load(object sender, EventArgs e) {  
  12.             if (!IsPostBack) {  
  13.                 grdibind();  
  14.             }  
  15.         }  
  16.         protected void grdibind() {  
  17.             Datatable dt = new Datatable();  
  18.             dt.Columns.Add("EmpId"typeof(string));  
  19.             dt.Columns.Add("EmpName"typeof(string));  
  20.             dt.Columns.Add("City"typeof(string));  
  21.             DataRow dr1 = dt.NewRow();  
  22.             dr1[0] = "L1Z 5F4";  
  23.             dr1[1] = "Nathaniel";  
  24.             dr1[2] = "Ilbono";  
  25.             dt.Rows.Add(dr1);  
  26.             DataRow dr2 = dt.NewRow();  
  27.             dr2[0] = "B7F 4U4";  
  28.             dr2[1] = "Ross";  
  29.             dr2[2] = "Dover";  
  30.             dt.Rows.Add(dr2);  
  31.             DataRow dr3 = dt.NewRow();  
  32.             dr3[0] = "R9C 1T9";  
  33.             dr3[1] = "Patrick";  
  34.             dr3[2] = "Edam";  
  35.             dt.Rows.Add(dr3);  
  36.             ViewState["GridData"] = dt;  
  37.             grdNormal.DataSource = dt;  
  38.             grdNormal.DataBind();  
  39.         }  
  40.         protected void lnkConvert_Click(object sender, EventArgs e) {  
  41.             Transpose();  
  42.         }  
  43.         protected void Transpose() {  
  44.             Datatable dt = (Datatable) ViewState["GridData"];  
  45.             Datatable newdt = new Datatable();  
  46.             //Create New Columns( count of columns will be equal to number of rows including header  
  47.             for (int i = 0; i <= dt.Rows.Count; i++) {  
  48.                 //Column Name string is not specified because column name will come in rows at start  
  49.                 newdt.Columns.Add("");  
  50.             }  
  51.             //Now Iterate through old Datatable and fill new Datatable   
  52.             //Create a new row in New Datatable   
  53.             for (int k = 0; k < dt.Columns.Count; k++) {  
  54.                 DataRow newdr = newdt.NewRow();  
  55.                 //Assign first column Value in NewDatatable with column name of old table  
  56.                 newdr[0] = dt.Columns[k].ColumnName.ToString();  
  57.                 for (int j = 0; j < dt.Rows.Count; j++) {  
  58.                     newdr[j + 1] = dt.Rows[j][k].ToString();  
  59.                 }  
  60.                 newdt.Rows.Add(newdr);  
  61.             }  
  62.             grdTranspose.DataSource = newdt;  
  63.             grdTranspose.DataBind();  
  64.         }  
  65.         protected void grdTranspose_RowCreated(object sender, Gridview RowEventArgs e) {  
  66.             if (e.Row.RowType == DataControlRowType.DataRow) {  
  67.                 e.Row.Cells[0].CssClass = "header";  
  68.             }  
  69.         }  
  70.     }  
  71. }  
Note: Actually here we are transposing Datatable and binding to Gridview.