In this example we
explain that how to convert gridview colouns to row or convert gridview columns
to gridview row in asp.net.
Sometime in project there are many clients that required the gridview data should be displayed in reverse format means gridview columns display as gridview row and gridview rows should be displayed as gridview columns.
Sometime in project there are many clients that required the gridview data should be displayed in reverse format means gridview columns display as gridview row and gridview rows should be displayed as gridview columns.
Read and Write in Text File in asp.Net Read and Write File in Asp.Net
So to fulfill the client request you have to use following code
Default2.aspx:-
<%@
Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>How
Convert Gridview Columns to Rows in Asp.net using c#</title>
<style type="text/css">
body
{
font-family:
Calibri;
}
.gridcss
{
background:
#da2045;
font-weight:
bold;
color:
White;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
<b>Default
Gridview</b>
</td>
<td>
</td>
<td>
<b>after
Convertion Gridview</b>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="gv_Defaultgrid" runat="server">
<HeaderStyle BackColor="#da2045" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</td>
<td>
</td>
<td>
<asp:GridView ID="gv_convertedgrid" runat="server" OnRowDataBound="gv_convertedgrid_RowDataBound">
</asp:GridView>
</td>
</tr>
</table>
</form>
</body>
</html>
Default2.aspx.cs:-
using System;
using System.Data;
using
System.Configuration;
using System.Web;
using
System.Web.Security;
using System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using System.IO;
using
System.Security.Cryptography;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
if
(!Page.IsPostBack)
{
PopulateGrid();
}
}
protected void PopulateGrid()
{
DataTable
dt = new DataTable();
dt.Columns.Add("Emp_Id",
typeof(Int32));
dt.Columns.Add("Emp_Name",
typeof(string));
dt.Columns.Add("Designation",
typeof(string));
DataRow
dtrow = dt.NewRow(); // Create First Row
dtrow["Emp_Id"]
= 1;
dtrow["Emp_Name"]
= "kirit";
dtrow["Designation"]
= "MCA";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); //
Create Second Row
dtrow["Emp_Id"]
= 2;
dtrow["Emp_Name"]
= "pintu";
dtrow["Designation"]
= "MSC";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); //
Create third Row
dtrow["Emp_Id"]
= 3;
dtrow["Emp_Name"]
= "rahul";
dtrow["Designation"]
= "CA";
dt.Rows.Add(dtrow);
gv_Defaultgrid.DataSource = dt;
gv_Defaultgrid.DataBind();
gv_convertedgrid.DataSource =
ConvertColumnsAsRows(dt);
gv_convertedgrid.DataBind();
gv_convertedgrid.HeaderRow.Visible = false;
}
protected void gv_convertedgrid_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
dt_temp = new DataTable();
//Convert all
the rows to columns
for (int i = 0; i <= dt.Rows.Count; i++)
{
dt_temp.Columns.Add(Convert.ToString(i));
}
DataRow
dr;
// Convert
All the Columns to Rows
for (int j = 0; j < dt.Columns.Count; j++)
{
dr = dt_temp.NewRow();
dr[0] = dt.Columns[j].ToString();
for
(int k = 1; k <= dt.Rows.Count; k++)
dr[k] = dt.Rows[k - 1][j];
dt_temp.Rows.Add(dr);
}
return
dt_temp;
}
}
0 comments:
Post a Comment