Display Rows as Columns in Gridview in Asp.net using C#


Here I will explain how to display gridview columns as rows in asp.net or display gridview rows as columns in asp.net using C#.

If you want change columns as rows we need to write code like as shown below
 
 
<head runat=”server”>
<title>Convert Gridview Columns as Rows in Asp.net</title>
<style type=”text/css”>
body
{
font-family:Calibri;
}
.gridcss
{
background:#df5015;
font-weight:bold;
color:White;
}
</style>
</head>
<body>
<form id=”form1″ runat=”server”>
<table>
<tr>
<td><b>Normal Gridview</b></td>
<td>&nbsp;&nbsp;</td>
<td><b>Converted Gridview</b></td>
</tr>
<tr>
<td>
<asp:GridView ID=”gvnormal” runat=”server”>
<HeaderStyle BackColor=”#df5015″ Font-Bold=”true” ForeColor=”White” />
</asp:GridView>
</td>
<td>&nbsp;&nbsp;</td>
<td>
<asp:GridView ID=”gvconverted” runat=”server” OnRowDataBound=gvconverted_RowDataBound>
</asp:GridView>
</td>
</tr>
</table>
</form>
</body>

</html>

Now in code behind add following namespace references
C# Code
 
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
After that write the following code in code behind
 
 
 
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridviewData();
}
}
protected void BindGridviewData()
{
DataTable dt = new DataTable();
dt.Columns.Add(“UserId”, typeof(Int32));
dt.Columns.Add(“UserName”, typeof(string));
dt.Columns.Add(“Education”, typeof(string));
dt.Columns.Add(“Location”, typeof(string));
DataRow dtrow = dt.NewRow();    // Create New Row
dtrow[“UserId”] = 1;            //Bind Data to Columns
dtrow[“UserName”] = “SureshDasari”;
dtrow[“Education”] = “B.Tech”;
dtrow[“Location”] = “Chennai”;
dt.Rows.Add(dtrow);
dtrow = dt.NewRow();               // Create New Row
dtrow[“UserId”] = 2;               //Bind Data to Columns
dtrow[“UserName”] = “MadhavSai”;
dtrow[“Education”] = “MBA”;
dtrow[“Location”] = “Nagpur”;
dt.Rows.Add(dtrow);
dtrow = dt.NewRow();              // Create New Row
dtrow[“UserId”] = 3;              //Bind Data to Columns
dtrow[“UserName”] = “MaheshDasari”;
dtrow[“Education”] = “B.Tech”;
dtrow[“Location”] = “Nuzividu”;
dt.Rows.Add(dtrow);
gvnormal.DataSource = dt;
gvnormal.DataBind();
gvconverted.DataSource = ConvertColumnsAsRows(dt);
gvconverted.DataBind();
gvconverted.HeaderRow.Visible = false;
}
protected void gvconverted_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].CssClass = “gridcss”;
}
}
// This function is used to convert columns to rows
public DataTable ConvertColumnsAsRows(DataTable dt)
{
DataTable dtnew=new DataTable();
//Convert all the rows to columns
for (int i = 0; i <= dt.Rows.Count; i++)
{
dtnew.Columns.Add(Convert.ToString(i));
}
DataRow dr;
// Convert All the Columns to Rows
for (int j = 0; j < dt.Columns.Count; j++)
{
dr = dtnew.NewRow();
dr[0] = dt.Columns[j].ToString();
for (int k = 1; k <= dt.Rows.Count; k++)
dr[k] = dt.Rows[k – 1][j];
dtnew.Rows.Add(dr);
}
return dtnew;
}
Advertisements