Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 18th, 2005, 07:51 PM
Authorized User
 
Join Date: Oct 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default How do I invert a datagrid, header on the right

I would like to know if it is possible to have static rows and dynamic columns. I want to do this because if I have static columns and dynamic rows then the user has to do massive horizontal scrolling.

I am working with asp.net and csharp, i was not sure if i should post here or in the csharp forum.

Thanks
Michael Hsu

 
Old July 19th, 2005, 03:37 AM
Authorized User
 
Join Date: Nov 2004
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to avanishp
Default

You can add columns in datagrid like that:

foreach(DataRow oRow in DS.Tables[0].Rows)
{
        oCol=new BoundColumn();
    oCol.HeaderText=CommonFunctions.GetRegString(oRow["Column"].ToString());
    oCol.DataField=oRow["Column"].ToString();
    oCol.HeaderStyle.CssClass="gridheader";
    oCol.SortExpression=oRow["Column"].ToString();
    oCol.ItemStyle.CssClass="gridfirstitem";
    InvoiceGrid.Columns.Add(oCol);
}

and then can set datatable to grids datasource.


Avanish Pandey
Set your heart upon your work, but never on its reward
 
Old July 19th, 2005, 12:57 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Mike,

Can you elaborate on "dynamic columns"? What are you hoping to achieve? It almost sounds like you are looking to pivet the table so that you get the rows from the data to show horizontally instead of vertically.

-Peter
 
Old July 19th, 2005, 04:46 PM
Authorized User
 
Join Date: Oct 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Can you elaborate on "dynamic columns"? What are you hoping to achieve? It almost sounds like you are looking to pivet the table so that you get the rows from the data to show horizontally instead of vertically.
-----------------------------------------------------------------
Yeah that's pretty much what I want.

 
Old July 20th, 2005, 09:44 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Ok, here's a clever (if I may say so) solution:

We nest a repeater inside of a repeater to manually construct the HTML table. The outer repeater (table rows) is bound to the table's columns collection (to pivet the source table columns to output table rows). The inner repeater (of cells) is bound to the rows of the table (to pivet source rows to columns).

We have to construct databinding syntax for the inner repeater to look back out at the bound object of the outer repeater (the source DataTable). This lets us get at the rows of that source table.

While we are looking backwards from the inner repeater we can see the RepeaterItem from the outer repeater that the inner repeater control is inside of. This allows us to see the object (DataColumn) that the outer repeater's RepeaterItem is bound to. Thus we can use this DataColumn to reference the current outer repeater's Column to get the corresponding value from the current row of the inner repeater.

Here's the markup:

<table border="1" cellspacing="0" cellpadding="2">
    <asp:repeater runat="server" id="rptRows">
        <itemtemplate>
            <tr>
                <td><%# Container.DataItem.ColumnName%></td>
                <asp:repeater runat="server" datasource='<%# Container.DataItem.Table.Rows %>'>
                    <itemtemplate>
                        <td><%# CType(Container.DataItem, System.Data.DataRow)(CType(Container.Parent.Parent , RepeaterItem).DataItem) %></td>
                    </itemtemplate>
                </asp:repeater>
            </tr>
        </itemtemplate>
    </asp:repeater>
</table>

Here's a breakdown of the databinding syntax for the data columns (best to look at this in the forum page to get the color coding):

CType(Container.DataItem, System.Data.DataRow)(CType(Container.Parent.Parent , RepeaterItem).DataItem)

Container: System.Web.UI.WebControls.RepeaterItem (Inner Repeater)
Container.DataItem: DataRow of the table rows iteration
Container.Parent: Inner Repeater
Container.Parent.Parent: System.Web.UI.WebControls.RepeaterItem (Outer Repeater)
CType(Container.Parent.Parent, RepeaterItem).DataItem: DataColumn (from outer repeater iteration)

This solution will work with any sized table because of the nested repeater.

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
Invert letter case martoufmarty Javascript How-To 4 June 8th, 2008 08:45 PM
Datagrid fixed header Swopna .NET Framework 1.x 1 March 22nd, 2008 11:06 AM
datagrid header frezing Rama C# 1 March 2nd, 2006 05:15 PM
header text in datagrid msrnivas .NET Web Services 1 March 4th, 2004 01:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.