Click here to Skip to main content
15,894,825 members
Articles / Programming Languages / C#
Article

Displaying Vertical Rows in DataGrid View

Rate me:
Please Sign up or sign in to vote.
3.40/5 (9 votes)
23 Jun 2007CPOL 99.3K   3.7K   42   3
Displaying vertical rows in DataGrid View

Screenshot - 1.jpg

Screenshot - 2.jpg

Introduction

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

Background

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:

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

    C#
    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" });
    
     table.AcceptChanges();
    
     return table;
    }
  5. Create OnLoad event and add the following code:

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

    C#
    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;
    }
  7. Add the button click event as follows:

    C#
    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)
    { 
      Application.Exit();
    }

Now build and run the application.

History

  • 23rd June, 2007: Initial post

License

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


Written By
Software Developer (Senior)
United Arab Emirates United Arab Emirates
Maybe its very difficult, but i am sure its not impossible.

Comments and Discussions

 
Generalthanks Pin
Gamil Mohamad25-Sep-10 8:36
Gamil Mohamad25-Sep-10 8:36 
GeneralAdjust Code To Nice Result Pin
blackeyes24-Sep-10 6:40
blackeyes24-Sep-10 6:40 
Adjust Your Code Like :

        private void butFlip_Click(object sender, EventArgs e)<br />
        {<br />
            DataSet new_ds = FlipDataSet(ds); // Flip the DataSet<br />
            DataView my_DataView = new_ds.Tables[0].DefaultView;<br />
            this.my_DataGrid.DataSource = my_DataView;<br />
<br />
            this.my_DataGrid.Columns[0].DefaultCellStyle.BackColor = Color.WhiteSmoke;<br />
            this.my_DataGrid.ColumnHeadersVisible = false;<br />
            this.my_DataGrid.RowHeadersVisible = false;<br />
<br />
            butFlip.Enabled     = false;<br />
            butNormal.Enabled   = true;<br />
        }<br />
<br />
        private void butNormal_Click(object sender, EventArgs e)<br />
        {<br />
            DataView my_DataView = ds.Tables[0].DefaultView;<br />
            this.my_DataGrid.DataSource = my_DataView;<br />
<br />
<br />
            this.my_DataGrid.ColumnHeadersVisible =  true ;<br />
            this.my_DataGrid.RowHeadersVisible =  true ;<br />
<br />
<br />
            butFlip.Enabled     = true;<br />
            butNormal.Enabled   = false;<br />
        }

GeneralNice job! Pin
speh14-Jul-08 10:34
speh14-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.