Click here to Skip to main content
12,250,257 members (55,802 online)
Click here to Skip to main content
Add your own
alternative version

Stats

224.9K views
51 bookmarked
Posted

Displaying vertical rows in a DataGrid

, 21 Sep 2003
Rate this:
Please Sign up or sign in to vote.
Displaying vertical rows in a datagrid by flipping the tables horizontally

Introduction

Last week, when I wanted to display a DataGrid in a horizontal manner (vertical rows), I thought there may be some property in DataGrid to flip it, but it wasn’t the case or may be I couldn’t find it. I read online articles and forums but didn’t get very useful or easy solutions. One of the solutions I found was to nest a DataList into a DataSet but I didn’t find this easy; and the other was all together a new control called xrepeater but I found mine easier than the two of them.

Let’s say you have a dataset which you want to render through a datagrid but you don’t want to show horizontal rows, instead you want to show the rows vertically. There may be lot of cases when you want to do that, for example comparison between two things on several criteria (Column headings) or when you have just one or two rows to show then you would prefer the vertical rows for more clarity. Here is how the idea of flipping the table comes in.

The idea is to flip all the entries inside each table of the dataset. This is not a very good solution but it works fine. I wrote a method which takes a DataSet as a parameter and returns a new DataSet with all tables flipped and now you just have to bind this DataSet’s tables to the DataGrid as you do normally and DataGrid renders vertical rows.

Old DataSet

Old Table looks like below.

Column1 Column2 Column3
Neeraj Jain Carios
Tashan Yen Agknow
Andrew Ferriere Feedback

Flipped DataSet

New DataTable looks like this

0 1 2 3
Column1 Neeraj Tashan Andrew
Column2 Jain Yen Agknow
Column3 Carios Agknow Feedback

Please note that in flipped table there are column headings as integer numbers, so when bind you it to the datagrid, and set the property ShowHeader=false; you are all set to render the vertical dataRows properly.

Code

Here is the code for FlipDataSet method, is in the code behind file for me. In method FlipDataSet, I am doing flipping of dataset and in BindData, I am binding the first table of it to the DataGrid. This is all:

private void BindData()
{
    DataSet ds = this.GetDetail(); // Some DataSet
    DataSet new_ds = FlipDataSet(ds); // Flip the DataSet
    DataView my_DataView = new_ds.Tables[0].DefaultView;
    this.my_DataGrid.DataSource = my_DataView;
    this.my_DataGrid.DataBind();
}


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;
        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;
}

Conclusion

Thanks for reading and please send any questions or comments you may have (I am sure you will).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Neeraj Jain
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralNot working for me Pin
_drifter19-Jan-11 11:40
member_drifter19-Jan-11 11:40 
GeneralRemoving Column names. Pin
srikantha_nagaraj9-Aug-09 20:02
membersrikantha_nagaraj9-Aug-09 20:02 
Questionhow can i modify the columnheader name 0 1 2 3 column? Pin
rkhiev22-Sep-08 10:50
memberrkhiev22-Sep-08 10:50 
GeneralThis Article very Good Pin
R4Ravi Kumar25-Feb-08 1:48
memberR4Ravi Kumar25-Feb-08 1:48 
GeneralGreat Pin
naveenj3-Jan-08 22:34
membernaveenj3-Jan-08 22:34 
GeneralSame thing Different approach! Pin
hengrr21-Aug-07 13:52
memberhengrr21-Aug-07 13:52 
GeneralDemo Project in C#.NET 2005 Pin
M. Gamal23-Jun-07 3:06
memberM. Gamal23-Jun-07 3:06 
GeneralThank you Pin
Baroon10-Mar-07 18:27
memberBaroon10-Mar-07 18:27 
GeneralEdited : Added Headerstyle, ItemStyle, and AlternatingStyles to columns [modified] Pin
beyerch22-Aug-06 10:24
memberbeyerch22-Aug-06 10:24 
QuestionRotate with nonString field Pin
Konstantin_STD24-Jul-06 2:23
memberKonstantin_STD24-Jul-06 2:23 
GeneralColumn Paging on this datagrid [modified] Pin
vaishika23-Jun-06 13:49
membervaishika23-Jun-06 13:49 
GeneralGridView instead of DataGrid Pin
yaip19-Jun-06 13:40
memberyaip19-Jun-06 13:40 
Generalcorrected code Pin
ZoneTrader17-Apr-06 12:46
memberZoneTrader17-Apr-06 12:46 
GeneralShort and sweet - Nice Pin
Jorge Da Silva7-Feb-06 8:38
memberJorge Da Silva7-Feb-06 8:38 
GeneralU need to alter one { curly brace Pin
cinni10-Jan-06 18:54
membercinni10-Jan-06 18:54 
QuestionHow do you format a column? Pin
ramsay_s29-Nov-05 6:51
memberramsay_s29-Nov-05 6:51 
GeneralNice job there! Pin
alkisman4-Sep-05 21:38
memberalkisman4-Sep-05 21:38 
GeneralVB Port Pin
brad_morga18-May-05 3:58
memberbrad_morga18-May-05 3:58 
QuestionRe: VB Port Pin
Member 436544317-Aug-08 20:19
memberMember 436544317-Aug-08 20:19 
GeneralRe: VB Port Pin
The Developer19-Aug-08 20:29
memberThe Developer19-Aug-08 20:29 
GeneralRe:Displaying vertical rows in a DataGrid Pin
Amr EL-Nasser29-Mar-05 3:51
sussAmr EL-Nasser29-Mar-05 3:51 
GeneralPush button,hyperlink or image button Pin
amit__85-Feb-05 1:49
memberamit__85-Feb-05 1:49 
GeneralRe: Push button,hyperlink or image button Pin
mailtoanzar11-Feb-05 18:19
membermailtoanzar11-Feb-05 18:19 
QuestionDid anyone got the .NEt Version working?? Pin
4netvb27-Jan-05 6:07
member4netvb27-Jan-05 6:07 
AnswerRe: Did anyone got the .NEt Version working?? Pin
M. Gamal4-Jul-07 14:49
memberM. Gamal4-Jul-07 14:49 
GeneralAlteration in Code Pin
Sumithrasembian12-Oct-04 4:27
memberSumithrasembian12-Oct-04 4:27 
GeneralRe: Alteration in Code Pin
dave.dolan31-Mar-09 9:50
memberdave.dolan31-Mar-09 9:50 
GeneralThank you thank you thank you! Pin
Anonymous18-Aug-04 5:29
sussAnonymous18-Aug-04 5:29 
GeneralHelp! Pin
Anonymous13-Aug-04 4:12
sussAnonymous13-Aug-04 4:12 
GeneralHelp! Pin
Anonymous13-Aug-04 4:11
sussAnonymous13-Aug-04 4:11 
GeneralAwsome work Pin
Chris Auer12-Aug-04 12:10
sussChris Auer12-Aug-04 12:10 
GeneralAwsome work Pin
Anonymous12-Aug-04 12:09
sussAnonymous12-Aug-04 12:09 
QuestionDatagrid structure? Pin
denpei14-May-04 6:40
memberdenpei14-May-04 6:40 
AnswerRe: Datagrid structure? Pin
denpei15-May-04 3:15
memberdenpei15-May-04 3:15 
Generalnice job... Pin
John Hodgkinson18-Mar-04 11:01
memberJohn Hodgkinson18-Mar-04 11:01 
GeneralRe: nice job... Pin
gmsm19-Mar-04 8:58
membergmsm19-Mar-04 8:58 
GeneralVB.Net version Pin
crodri13-Feb-04 11:05
membercrodri13-Feb-04 11:05 
GeneralRe: VB.Net version Pin
Anonymous14-Mar-04 14:45
sussAnonymous14-Mar-04 14:45 
GeneralRe: VB.Net version Pin
GanesanM3-May-04 4:35
memberGanesanM3-May-04 4:35 
GeneralRe: VB.Net version Pin
Geeta Yadav23-Sep-04 0:47
sussGeeta Yadav23-Sep-04 0:47 
GeneralRe: VB.Net version Pin
stsong5-Nov-04 4:30
memberstsong5-Nov-04 4:30 
Questionextra bracket? Pin
Anonymous10-Oct-03 10:36
sussAnonymous10-Oct-03 10:36 
AnswerRe: extra bracket? Pin
Neeraj Jain12-Oct-03 21:18
memberNeeraj Jain12-Oct-03 21:18 
GeneralRe: extra bracket? Pin
Anonymous14-Oct-03 9:59
sussAnonymous14-Oct-03 9:59 
GeneralNice job. Pin
GanesanM3-May-04 4:40
memberGanesanM3-May-04 4:40 
GeneralAlternative Pin
George L. Jackson22-Sep-03 10:11
memberGeorge L. Jackson22-Sep-03 10:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160426.1 | Last Updated 22 Sep 2003
Article Copyright 2003 by Neeraj Jain
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid