Jitesh Byahut 3/26/2014 4244

How to Bind GridView In Asp.Net

Here we are binding a GridView in Asp.Net.

Here we are using Column BoundField for Changing header name.

Points Of Remember:

1. Add namespace System.Data and System.Data.SqlClient in your C# page.

<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Bind GridView in Asp.net</title>



    <form id="form1" runat="server">


        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"

                      CellPadding="4" ForeColor="#333333">


                    <asp:BoundField DataField="Name" HeaderText="Student Name" />

                    <asp:BoundField DataField="Branch" HeaderText="Branch" />

                    <asp:BoundField DataField="City" HeaderText="City Name" />               


            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />       

            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />






Here we are using ADO.Net to bind student table values into GridView.

C# Codes :

using System.Data;

using System.Data.SqlClient;

public partial class GridView : System.Web.UI.Page


    protected void Page_Load(object sender, EventArgs e)


        if (!IsPostBack)





    //method for binding GridView

    protected void BindGridView()


        DataTable dt = new DataTable();

        SqlConnection con =  new SqlConnection(@"Data Source=JITESH-PC\SQL;Initial                                  Catalog=db_Student;Integrated Security=True");

        SqlDataAdapter da = new SqlDataAdapter("Select Name,Branch,City from tbl_student",con);





        if (dt.Rows.Count > 0)


            GridView1.DataSource = dt;





View output :


Please give your feedback for improving this page