Displaying vertical rows in a GridView In ASP.NET

In this post, I will show you a simple and easy way to display the GridView record Vertical. Check out the following code snippet, which is self-explanatory.
[]

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FlipGridView.aspx.cs" Inherits="XmlPostData" %>  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
<title>Untitled Page</title>  
</head>  
<body>  
<form id="form1" runat="server">  
    <div>  
        <asp:GridView ID="GridView1" runat="server" ShowHeader="false" AllowSorting="true">  
        </asp:GridView>  
    </div>  
</form>  
</body>  
</html>  
using System;  
using System.Data;  
using System.Configuration;  
using System.Collections;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Web.UI.HtmlControls;  
using System.Net;  
using System.Text;  
using System.IO;  

public partial class FlipGridView:System.Web.UI.Page  
{  
  protected void Page_Load(object sender, EventArgs e)  
  {  
   
      GridView1.DataSource = FlipDataSet(c());  
      GridView1.DataBind();  

  }  

  public DataSet c()  
  {  
      DataSet ds = new DataSet();  
      DataTable dt = new DataTable("Company");  
      DataRow dr;  
      dt.Columns.Add(new DataColumn("accountNo", typeof(Int32)));  
      dt.Columns.Add(new DataColumn("CompanyName", typeof(string)));  
      dt.Columns.Add(new DataColumn("Address", typeof(string)));  
      for (int i = 0; i <= 10; i++)  
      {  
          dr = dt.NewRow();  
          dr[0] = i;  
          dr[1] = "Company" + i + Environment.NewLine + "Title" + i;  
          dr[2] = "Address" + i + Environment.NewLine + "Title" + i;  
          dt.Rows.Add(dr);  
      }  
      ds.Tables.Add(dt);  
      return ds;  
  }  
  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 = null;  
          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;  
  }  
}

Please do not post any spam link in the comment box😊

Post a Comment (0)
Previous Post Next Post