How To Display vertical record in GridView

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

<!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">
           </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;

public partial class VerticalData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {


            GridView1.DataSource = GoDoReShape(_sampleData);
            GridView1.DataBind();
        }

    }
    public DataTable _sampleData
    {
        get
        {
            DataTable dt = (DataTable)Session["DataTable"];
            if (dt == null)
            {
                dt = new DataTable();
                dt.Columns.Add(new DataColumn("ID", typeof(string)));
                dt.Columns.Add(new DataColumn("Item", typeof(string)));
                dt.Columns.Add(new DataColumn("Price", typeof(string)));


                dt.Rows.Add(new object[] { "1", "Product1", "12.43", });
                dt.Rows.Add(new object[] { "2", "Product2", "11.43", });
                dt.Rows.Add(new object[] { "3", "Product3", "13.43", });
                Session["DataTable"] = dt;
            }
            return dt;
        }
        set
        {
            Session["DataTable"] = value;
        }
    }
    public DataTable GoDoReShape(DataTable dt)
    {
        DataTable NewDt = new DataTable();
        //Create Two Columns with names "ColumnName" and "Value"
        //ColumnName -> Displays all ColumnNames
        //Value -> Displays ColumnData
        NewDt.Columns.Add("ColumnName");
        NewDt.Columns.Add("Value");
        foreach (DataRow dr in dt.Rows)
        {
            foreach (System.Data.DataColumn dcol in dt.Columns)
            {
                //Declare Array
                string[] MyArray ={ dcol.ColumnName.ToString(), dr[dcol.ColumnName.ToString()].ToString() };
                NewDt.Rows.Add(MyArray);
            }
        }
        return NewDt;
    }



}

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

Post a Comment (0)
Previous Post Next Post