Click here to Skip to main content
14,735,421 members
Articles » Database » Database » General
Posted 21 Sep 2003


51 bookmarked

Displaying Vertical Rows in a DataGrid

Rate me:
Please Sign up or sign in to vote.
4.46/5 (40 votes)
21 Sep 2003
Displaying vertical rows in a datagrid by flipping the tables horizontally
This is the solution that I came up with to display a DataGrid in a horizontal manner (vertical rows). 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.


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.


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;

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++)
        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];
return ds;


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


  • 22nd September, 2003: Initial version


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.


About the Author

Neeraj Jain
Web Developer
United States United States
No Biography provided

Comments and Discussions

GeneralNot working for me Pin
_drifter19-Jan-11 12:40
Member_drifter19-Jan-11 12:40 
GeneralRemoving Column names. Pin
srikantha_nagaraj9-Aug-09 21:02
Membersrikantha_nagaraj9-Aug-09 21:02 
Questionhow can i modify the columnheader name 0 1 2 3 column? Pin
rkhiev22-Sep-08 11:50
Memberrkhiev22-Sep-08 11:50 
QuestionHow we can perform Update on this transposed grid Pin
msaqu2-Aug-08 21:25
Membermsaqu2-Aug-08 21:25 
GeneralThis Article very Good Pin
RRavi Kumar25-Feb-08 2:48
MemberRRavi Kumar25-Feb-08 2:48 
GeneralGreat Pin
naveenj3-Jan-08 23:34
Membernaveenj3-Jan-08 23:34 
GeneralSame thing Different approach! Pin
hengrr21-Aug-07 14:52
Memberhengrr21-Aug-07 14:52 
GeneralDemo Project in C#.NET 2005 Pin
Gamil Mohamad23-Jun-07 4:06
MemberGamil Mohamad23-Jun-07 4:06 
GeneralThank you Pin
Baroon10-Mar-07 19:27
MemberBaroon10-Mar-07 19:27 
GeneralEdited : Added Headerstyle, ItemStyle, and AlternatingStyles to columns [modified] Pin
beyerch22-Aug-06 11:24
Memberbeyerch22-Aug-06 11:24 
QuestionRotate with nonString field Pin
Konstantin_STD24-Jul-06 3:23
MemberKonstantin_STD24-Jul-06 3:23 
GeneralColumn Paging on this datagrid [modified] Pin
vaishika23-Jun-06 14:49
Membervaishika23-Jun-06 14:49 
GeneralGridView instead of DataGrid Pin
yaip19-Jun-06 14:40
Memberyaip19-Jun-06 14:40 
Generalcorrected code Pin
ZoneTrader17-Apr-06 13:46
MemberZoneTrader17-Apr-06 13:46 
GeneralShort and sweet - Nice Pin
Jorge Da Silva7-Feb-06 9:38
MemberJorge Da Silva7-Feb-06 9:38 
GeneralU need to alter one { curly brace Pin
Cinni10-Jan-06 19:54
MemberCinni10-Jan-06 19:54 
QuestionHow do you format a column? Pin
ramsay_s29-Nov-05 7:51
Memberramsay_s29-Nov-05 7:51 
GeneralNice job there! Pin
alkisman4-Sep-05 22:38
Memberalkisman4-Sep-05 22:38 
GeneralVB Port Pin
brad_morga18-May-05 4:58
Memberbrad_morga18-May-05 4:58 
QuestionRe: VB Port Pin
Member 436544317-Aug-08 21:19
MemberMember 436544317-Aug-08 21:19 
GeneralRe: VB Port Pin
The Web Developer19-Aug-08 21:29
MemberThe Web Developer19-Aug-08 21:29 
GeneralRe:Displaying vertical rows in a DataGrid Pin
Amr EL-Nasser29-Mar-05 4:51
MemberAmr EL-Nasser29-Mar-05 4:51 
GeneralPush button,hyperlink or image button Pin
amit__85-Feb-05 2:49
Memberamit__85-Feb-05 2:49 
GeneralRe: Push button,hyperlink or image button Pin
miltash11-Feb-05 19:19
Membermiltash11-Feb-05 19:19 
QuestionDid anyone got the .NEt Version working?? Pin
youngoz27-Jan-05 7:07
Memberyoungoz27-Jan-05 7:07 

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.