Wednesday, April 9, 2008

Displaying vertical rows in a GridView







<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FlipGridView.aspx.cs" Inherits="XmlPostData" %>

<!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 runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" ShowHeader="false" AllowSorting="true">
</asp:GridView>
</div>
</form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Net;
using System.Text;
using System.IO;

public partial class FlipGridView:System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

GridView1.DataSource = FlipDataSet(c());
GridView1.DataBind();

}

public DataSet c()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable("Company");
DataRow dr;
dt.Columns.Add(new DataColumn("accountNo", typeof(Int32)));
dt.Columns.Add(new DataColumn("CompanyName", typeof(string)));
dt.Columns.Add(new DataColumn("Address", typeof(string)));
for (int i = 0; i <= 10; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Company" + i + Environment.NewLine + "Title" + i;
dr[2] = "Address" + i + Environment.NewLine + "Title" + i;
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
return ds;
}
public DataSet FlipDataSet(DataSet my_DataSet)
{
DataSet ds = new DataSet();
foreach (DataTable dt in my_DataSet.Tables)
{
DataTable table = new DataTable();
for (int i = 0; i <= dt.Rows.Count; i++)
{
table.Columns.Add(Convert.ToString(i));
}
DataRow r = null;
for (int k = 0; k < dt.Columns.Count; k++)
{
r = table.NewRow();
r[0] = dt.Columns[k].ToString();
for (int j = 1; j <= dt.Rows.Count; j++)
r[j] = dt.Rows[j - 1][k];
table.Rows.Add(r);
}
ds.Tables.Add(table);
}

return ds;
}
}

14 comments:

Anonymous said...

Great..
Thanks Bro..

Anonymous said...

I love it!
Better than great, I implemented the code with absolutely no hassles.
Many thanks.

kevv said...

is there a way of selecting/editing/deleting a vertical row ?

Anonymous said...

hi
can i add first row ( Horizantal ) then other rows under it ( virtical ) ?
for ex: the first row will contain customersName , then i need column under each customer conatine some data

Anonymous said...

I am not sure about this.I will check and let you know.

Anonymous said...

ok thanks but please help me :)

Anonymous said...

Thanks a lot, it worked like a charm! :)

Anonymous said...

Thanks a lot dear

Anonymous said...

great super
thanks

shwetha said...

this gridview binding for only single table or multiple tables?

santosh said...

@Shwetha:you can use multiple table

shwetha said...

How to take multiple table names can u pls help me..

shwetha said...

I want to display my gridview with the folloing details....
dayname | Hour1 |Hour2 |Hour3
Mon |tel |hin |eng
Tue |Hin |eng |Maths
Like this for all Daynames...
Where Subject selection Based on this DayName and HourName from SubjectTable.
In this we Have to display DayName from DayTable, HourNames From Hour Table and one more thing in this hourName displayed like as vertical column...
I need to establish this in my project could you pls provide some help to me...

santosh said...

@shwetha:Do you have snapshot?,if yes then please send it to me (santosh.ksingh03@gmail.com)

Post a Comment