Creating Gridview Using Asp.net and C#

Hi,
Here I will code you how to bind the data into Gridview using C# and Asp.net.  This is very basic grid with binding all the columns retrieved from the select Query. Gridview is said to be one of the best control from the Microsoft.net.

Output Preview
Basic Gridview in C#

 Database Structure


ASPX CODE :


<html>
<head runat="server">
    <title>Fourthbottle</title>
</head>
<body>
    <form id="form1" runat="server">
    <div  style=" width:100%; text-align :center">
    <asp:GridView ID="GrdView" runat="server">
    </asp:GridView>
    <asp:Label id="lblstatus" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>


C# CODE :
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace GRD
{
    public partial class BasicGridV : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }
        protected void BindGrid()
        {
            SqlConnection con = new SqlConnection("Your Connection String");
            string Query = "select * from Table1";
            SqlDataAdapter da = new SqlDataAdapter(Query, con);
            DataSet DS = new DataSet();
            try
            {
                con.Open();
                da.Fill(DS);
                GrdView.DataSource = DS;
                GrdView.DataBind();
                con.Close();

            }
            catch (Exception es)
            {
                lblstatus.Text = es.Message.ToString();
            }
        }
    }
}