Click here to Skip to main content
13,044,551 members (62,603 online)
Click here to Skip to main content
Add your own
alternative version


43 bookmarked
Posted 23 Jun 2007

Displaying Vertical Rows in DataGrid View

, 23 Jun 2007
Rate this:
Please Sign up or sign in to vote.
Displaying vertical rows in DataGrid View

Screenshot - 1.jpg

Screenshot - 2.jpg


This article is just an example of Neeraj Jain's work in Displaying Vertical Rows in a DataGrid.


Here I designed a C# application with a DataGridView containing some information from a DataTable object in the normal mode (Horizontal Rows). When you press on "Flip Datagride" button, the data grid displays the same table but in the Flipped mode (Vertical Rows).

Using the Code

  1. Create a new C# .NET 2005 Windows Application.

  2. Add the buttons and the DataGridView object on the Form as shown in the picture.

  3. Declare the DataSet and DataTable object as follows:

    public partial class Form1 : Form
       DataSet   ds = null;
       DataTable dt = null;
       public Form1()
  4. Create a new method that creates and returns a DataTable object filled with some data:

    private static DataTable GetCustomers()
     DataTable table = new DataTable();
     table.TableName = "Customers";
     table.Columns.Add("Name", typeof(string));
     table.Columns.Add("Price", typeof(string));
     table.Columns.Add("Country", typeof(string));
     table.Rows.Add(new object[] { "Mohamad", "1700", "Egypt" });
     table.Rows.Add(new object[] { "Tarek", "550", "Syria" });
     table.Rows.Add(new object[] { "Gamal", "762", "Saudi Arabia" });
     return table;
  5. Create OnLoad event and add the following code:

    private void Form1_Load(object sender, EventArgs e)
      ds = new DataSet();
      dt = new DataTable();
      dt = GetCustomers();
      DataView my_DataView = ds.Tables[0].DefaultView;
      this.my_DataGrid.DataSource = my_DataView;
  6. Create the FlipDataSet method:

    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]; }
     return ds;
  7. Add the button click event as follows:

    private void butFlip_Click(object sender, EventArgs e)
      DataSet new_ds = FlipDataSet(ds); // Flip the DataSet
      DataView my_DataView = new_ds.Tables[0].DefaultView;
      this.my_DataGrid.DataSource = my_DataView;
      butFlip.Enabled = false;
      butNormal.Enabled = true;
    private void butNormal_Click(object sender, EventArgs e)
      DataView my_DataView = ds.Tables[0].DefaultView;
      this.my_DataGrid.DataSource = my_DataView;
      butFlip.Enabled = true;
      butNormal.Enabled = false;
    private void butExit_Click(object sender, EventArgs e)

Now build and run the application.


  • 23rd June, 2007: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mohamad Gamal
Software Developer (Senior)
Egypt Egypt
Maybe its very difficult, but i am sure its not impossible.

You may also be interested in...

Comments and Discussions

Generalthanks Pin
Mohamad Gamal25-Sep-10 8:36
memberMohamad Gamal25-Sep-10 8:36 
GeneralAdjust Code To Nice Result Pin
blackeyes24-Sep-10 6:40
memberblackeyes24-Sep-10 6:40 
GeneralNice job! Pin
speh14-Jul-08 10:34
memberspeh14-Jul-08 10:34 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 23 Jun 2007
Article Copyright 2007 by Mohamad Gamal
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid