rotate a datagrid

J

Joey

hi everyone

is it possible to rotate a datagrid, so that the columns become rows and
the rows columns?

so the datagrid shows like

-----------------
| header | data |
-----------------
| header | data |
-----------------

or is there another way to make columns editable instead of rows ?

any help appreciated,

Joey
 
A

Alvin Bruney [MVP]

here is some code:

Sometimes it is necessary to flip a datagrid's rows with its columns. Here
is the code:

Let's assume the information is returned in a dataset called ds. Here is how
we fix that puppy.



DataSet dsTemp = new DataSet();

DataTable Tables = new DataTable();

dsTemp.Tables.Add(Tables);



dsTemp.Tables[0].Columns.Add( " ", System.Type.GetType( "System.String" ) );

dsTemp.Tables[0].Columns.Add( "% variance", System.Type.GetType(
"System.String" ) );

..

..

..

[snip]



try

{

for(int col = 0; col <
ds.Tables[0].Columns.Count; col++)

{

DataRow myRow =
dsTemp.Tables[0].NewRow();

myRow[1]=
ds2.Tables[0].Rows[0][col].ToString();

myRow[0]=
ds.Tables[0].Columns[col].ColumnName;

..

..

..

[snip]



dsTemp.Tables[0].Rows.Add(myRow);

}

}

catch(Exception ex)

{

[snip]

}



if(ds.Tables[0].Rows.Count > 0)

{

// FlippedDataGrid. Is our datagrid

FlippedDataGrid.DataSource =
dsTemp;

FlippedDataGrid..DataBind();



}
 
S

Saravana [MVP]

Hi,

I thing its better to get the data in whatever format you need it from
datasource(for example sql server) instead of doing that with dataset.
 
S

Steven Cheng[MSFT]

Hi Joey,

In addition to Saravana, Alvin's suggestion on do the rotate at the
datasource level( switch the datas in the DataSet), here are my suggestions;

As for your condition,you're not only want to display horizentally, but
also perform the horizental mode at edit mode, so I think the DataGrid web
server control is not very suitable. I'd recommend you try the DataList
control
The DataList Control has a "RepeatDirection" property which is used to
speicify the direction of the repeated items in the DataList, we can assign
the "RepeatDirection.Horizontal" as its value so as to let the items
repeated horizentally. Also, we need to customize the ItemTemplate and
EditTemplate and manually add the CommandButtons in DataList. For detailed
reference , you can view the following link in MSDN:
#DataList.RepeatDirection Property
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwebuiwebcontro
lsdatalistclassrepeatdirectiontopic.asp?frame=true

To make it clearly, here is a simple demo page, you may have a look :

==================aspx page======================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DataList</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td><asp:datalist id="dlMain" runat="server"
RepeatDirection="Horizontal">
<HeaderTemplate>
<table align="center" width="100%">
<tr>
</HeaderTemplate>
<FooterTemplate>
</tr>
</table>
</FooterTemplate>
<ItemTemplate>
<td>
<table>
<tr>
<td><%# DataBinder.Eval(Container.DataItem,"index") %></td>
</tr>
<tr>
<td><%# DataBinder.Eval(Container.DataItem,"name") %></td>
</tr>
<tr>
<td><%# DataBinder.Eval(Container.DataItem,"email") %></td>
</tr>
<tr>
<td><asp:Button ID="btnEdit" Runat="server" Text="Edit"
CommandName="Edit" CommandArgument='<%# Container.ItemIndex
%>'></asp:Button></td>
</tr>
</table>
</td>
</ItemTemplate>
<EditItemTemplate>
<td>
<table>
<tr>
<td><asp:TextBox ID="txtIndex" Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem,"index") %>'></asp:TextBox></td>
</tr>
<tr>
<td><asp:TextBox ID="txtName" Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem,"name") %>'></asp:TextBox></td>
</tr>
<tr>
<td><asp:TextBox ID="txtEmail" Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem,"email") %>'></asp:TextBox></td>
</tr>
<tr>
<td>
<asp:Button ID="btnUpdate" Runat="server" Text="Update"
CommandName="Update" CommandArgument='<%# Container.ItemIndex %>'>
</asp:Button>
<asp:Button ID="btnCancel" Runat="server" Text="Cancel"
CommandName="Cancel" CommandArgument='<%# Container.ItemIndex %>'>
</asp:Button>
</td>
</tr>
</table>
</td>
</EditItemTemplate>
</asp:DataList></TD></TR>
<tr>
<td></td>
</tr>
</TABLE></form>
</body>
</HTML>

================code behind page classs============
public class DataList : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList dlMain;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Bind_Data();
}
}

private void Bind_Data()
{
DataTable tb = new DataTable("user");
tb.Columns.Add("index",typeof(int));
tb.Columns.Add("name",typeof(string));
tb.Columns.Add("email",typeof(string));

DataRow dr = null;

for(int i=1;i<=6;i++)
{
dr = tb.NewRow();
dr["index"] = i;
dr["name"] = "Name" + i;
dr["email"] = "User" + i + "@test.com";
tb.Rows.Add(dr);
}


dlMain.DataSource = tb;
dlMain.DataBind();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dlMain.CancelCommand += new
System.Web.UI.WebControls.DataListCommandEventHandler(this.dlMain_CancelComm
and);
this.dlMain.EditCommand += new
System.Web.UI.WebControls.DataListCommandEventHandler(this.dlMain_EditComman
d);
this.dlMain.UpdateCommand += new
System.Web.UI.WebControls.DataListCommandEventHandler(this.dlMain_UpdateComm
and);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion





private void dlMain_EditCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
dlMain.EditItemIndex = e.Item.ItemIndex;
Bind_Data();
}

private void dlMain_UpdateCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
dlMain.EditItemIndex = -1;
Bind_Data();
}

private void dlMain_CancelCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
dlMain.EditItemIndex = -1;
Bind_Data();
}
}
====================================================

Hope these helps. Thanks.



Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
S

Steven Cheng[MSFT]

Hi Joey,

Have you had a chance to check out the suggestions in my last reply or have
you got any further ideas on this issue? If you have anything unclear or if
there're anything else we can help, please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top