Click here to Skip to main content
Licence 
First Posted 10 Aug 2003
Views 148,031
Bookmarked 61 times

Display a SQL row vertically and column horizontally

By | 15 Aug 2003 | Article
This article demonstrates how to flip and display rows and columns of a DataSet table.

Sample screenshot

Introduction

For displaying data in a table, DataGrid probably is the best choice. However, sometimes you may find it necessary to render your table in a different manner. Say, when you have a database table that has more than twenty fields and only a couple of records, I bet you would want to display all the rows vertically and the columns horizontally. Actually a week ago I stumbled into this problem myself. Checking online, I found some users had the same problem, yet there is not a workable answer ready for sharing. Therefore I present my solution below. It may not be the best answer, but it certainly is quick and simple to me.

The code

Solution 1

For saving the trouble of running SQL scripts to recreate a table, I created a .xml file for use. But the code works perfectly with any database table too.

Briefly speaking, the code first reads a .xml file into a DataSet, then saves all data into a two dimensional array. The size of array is dynamically decided by the size of the DataSet table. After the array is populated with data, we build up a table programmatically by adding TableRows and TableCells.

    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.XML" %>

    <html>

    <script language="VB" runat="server">


        Sub Page_Load(Sender As Object, E As EventArgs)
            Dim dataArray(,) as string
            dim i, j as integer
        

        'Create instance of DataSet object
            
              dim DS as DataSet = new DataSet()
            DS.ReadXml(Server.MapPath("Employees.xml"))
            
            dim rowCount as integer= DS.Tables(0).Rows.Count
            dim colCount as integer= DS.Tables(0).Columns.Count 
            
            redim dataArray(rowCount, colCount)
            
            'save data in a two dimensional array
            
            for i = 0 to rowCount -1
                for j = 0 to colCount -1
                    if DS.Tables(0).Rows(i).item(j) is DBNull.value
                        dataArray(i,j)=""
                    else
                        dataArray(i,j) =DS.Tables(0).Rows(i).item(j)

                    end if
                next 
            next
            
            'Switch columns and rows, dynamically filling in the table
            
            dim r as TableRow
            dim c as TableCell
            
            for j = 0 to colCount -1
                r = new TableRow()
                c = new TableCell()
                c.Controls.Add(new _
                  LiteralControl(DS.Tables(0).Columns(j).ColumnName))
                c.VerticalAlign = VerticalAlign.Top
                c.Style("background-color") = "lightblue"
                r.cells.add(c)    
                for i = 0 to rowCount - 1
                    
                    c = new TableCell()
                    c.Controls.Add(new LiteralControl(dataArray(i,j)))
                    c.VerticalAlign = VerticalAlign.Top
                    r.cells.add(c)
                next i
            
            Table1.Rows.Add(r)
            next j
                
            
        End Sub

    </script>
    <center>
    <h3> Display Employee Table Vertically</h3>
    </center>
    <body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">

    <form runat="server" ID="Form1">
    <asp:Table id="Table1" Font-Name="Verdana" Font-Size="8pt" 
        CellPadding=3 CellSpacing=0 BorderColor="black" 
        BorderWidth="1" Gridlines="Both" runat="server"/>
    </form>

    </body>
    </html>    

Solution 2

This solution, using XSL to transform XML into HTML in a format desired, is based on a user's suggestion (Thanks). The following is part of the hard-coded XSL file.

       <TABLE border="1">
       <COLGROUP WIDTH="100" ALIGN="CENTER" bgcolor="lavender"></COLGROUP>
      <TR><TD>EmployeeID</TD>
          <xsl:for-each select="NewDataSet/Table">
              <TD><xsl:value-of select="EmployeeID"/></TD>
           </xsl:for-each>
      </TR>
      <TR><TD>LastName</TD>
       <xsl:for-each select="NewDataSet/Table">
              <TD><xsl:value-of select="LastName"/></TD>
          </xsl:for-each>
      </TR>
         ...

To use the XSL file, we need to add XSL reference to the original XML file:

<?xml-stylesheet type="text/xsl" href="Employees.xsl"?>

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

About the Author

Xun Ding



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionC# code available? Pinmembersanme9818:22 12 May '04  
AnswerRe: C# code available? PinmemberXun Ding10:35 13 May '04  
GeneralRe: C# code available? Pinmembersanme9816:59 13 May '04  
GeneralRe: C# code available? PinmemberXun Ding17:17 13 May '04  
AnswerRe: C# code available? PinmemberXun Ding12:01 14 May '04  
GeneralRe: C# code available? Pinmembersanme9816:28 16 May '04  
GeneralRe: C# code available? PinmemberNavvy13:32 17 Jun '04  
GeneralRe: C# code available? PinmemberXun Ding11:37 18 Jun '04  
AnswerC# code available! Pinmember_Groker5:18 4 May '07  
GeneralGreat Work!! Pinmemberbonniew4:17 24 Feb '04  
QuestionXSL ? PinmemberSébastien Lorion13:21 16 Aug '03  
AnswerRe: XSL ? Pinmembercurry17:13 16 Aug '03  
GeneralRe: XSL ? PinmemberSébastien Lorion19:34 16 Aug '03  
GeneralRe: XSL ? Pinmembercurry11:11 17 Aug '03  
GeneralRe: XSL ? PinmemberSébastien Lorion11:57 17 Aug '03  
QuestionFix ColumnNames? Pinmemberspieler21:26 12 Aug '03  
AnswerRe: Fix ColumnNames? PinsussAnonymous4:07 13 Aug '03  
AnswerRe: Fix ColumnNames? Pinmembercurry8:15 13 Aug '03  
GeneralWindows Forms PinsussAnonymous6:38 12 Aug '03  
GeneralExecellent coder PinsussAnonymous17:59 11 Aug '03  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 16 Aug 2003
Article Copyright 2003 by Xun Ding
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid