Skip to content
Loading
how to show the data in GRID VIEW 
  • Yogesh Kumar
    Its very simple to show the data in GridView from DataBase Table.
    First you have to drag gridview from the Data control to your page after that you have writing only  the code it will automatically fetch your database table.
    or you can progammatically write the code of gridview {
    }
    in source code:
     
    If you want they appear in different way you can change it  yourself you have to only uncheck the auto generated field or maually change in the properties of griedview AutoGenerateColumns="True" to AutoGenerateColumns="False"
     
     
     
    code:
     
     You have many option in the gridview properties to change color style and other properties.
     
     I am using here Stored Procedure to Connect with DataBase if you want any help or want to understand how stored procedure created and used in ASP.NET than go to:
     
    https://www.c-sharpcorner.com/forums/stored-procedure-in-adonet2 
     
     
     
    code for connect the gridview:
     
     
    using System;
    using System.Data;
    using System.Data.SqlClient;
    public partial class Bound : System.Web.UI.Page
    {
    SqlConnection cn;
    SqlDataAdapter da;
    SqlCommand cm;
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
    cn = new SqlConnection(@"data source=CSMBHUL120\SQL2012;initial catalog=Vaahan;user id=sa;password=csmpl@123");
    cn.Open();
    cm = new SqlCommand();
    cm.Connection = cn;
    cm.CommandType = CommandType.StoredProcedure;
    cm.CommandText = "RCBook_Select_All";
    da = new SqlDataAdapter(cm);
    ds = new DataSet();
    ds.Clear();
    ds.Reset();
    da.Fill(ds);
    GridView1.DataSource = ds.Tables[0];
    GridView1.DataBind();
    }
    }
  • Sanwar Ranwa
    Hello Shreyas
     
    Please check these links 
     
    https://www.c-sharpcorner.com/UploadFile/b43a5b/retrieving-data-in-gridview-in-Asp-Net/
     
    https://www.c-sharpcorner.com/blogs/binding-data-from-database-to-grid-view-in-asp-net
     
    https://www.aspsnippets.com/Articles/Display-data-in-GridView-from-database-in-ASPNet-using-C-and-VBNet.aspx 
  • Hardik Deshani
    Try below link for complete code of how to bind GridView in aspx page
     
    https://atozsourcecode.com/BlogPost/Index/7a806c03-7a19-47b8-9f32-9367719ac078/how-to-bind-serverside-control-gridview-aspnet-csharp
     
    Hope this will help.
  •  aspx page : 
     
     
    BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"
    CellPadding="4" DataKeyNames="id">
     
     
     .cs Page coding 
     
    public partial class Default2 : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    BindData();
    }
    }
    public void BindData()
    {
    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
    SqlCommand cmd = new SqlCommand("select * from tbl_data", con);
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    sda.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
    }
    }