Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

GridView Control Beginners Guide

Author: DevASP
Download Source Code : 712_DatagridSample.zip

In this article I will try to explain you the basics of binding data to GridView Control. Here I have created application using ASP.Net 2.0. This article will be helpful for beginners.

GridView is among the basic control for binding data and displaying data on web form. When ever we fetch record from database we usually prefer gridview control for displaying our data. So here I will cover the basic of it. To begin with application, select GridView control from toolbox from the left pane of Visual studio editor. You will find the gridview control in data control category. Simply drag and drop on your web form. Now set its properties that is, it’s ID=IDs, runat=server and AutoGenerateColumn=false. Add columns using Column tag, add templates using ItemTemplate tag. Your aspx code for your web form will be look like as follows:

 

            <asp:GridView ID="gr" AutoGenerateColumns="false" runat="server">

                <Columns>

                    <asp:TemplateField HeaderText="Employee Name" >

                        <ItemTemplate>

                            <asp:Label ID="lblLabel" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "EmpFirstName").ToString() + " " + DataBinder.Eval(Container.DataItem, "EmpLastName").ToString() %>'></asp:Label>

                        </ItemTemplate>

                    </asp:TemplateField>                   

                    <asp:TemplateField HeaderText="Age">

                        <ItemTemplate>

                            <asp:Label ID="lblLabel2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Age" ) %>'></asp:Label>

                        </ItemTemplate>

                    </asp:TemplateField>

                </Columns>

            </asp:GridView>

 

Note the headertext property set the text of your column header. DataBinder.Eval(Container.DataItem, "EmpFirstName") this will display text of your EmpFirstName column. Basically this binds your selected column text with control text which is being used for displaying data or it can be used without any control upto user/developer.

 

Now we come to code behind code. Declare three global objects, first is Connection object, second is Adapter object and last is Dataview object. The namespace used for accessing the sqlconnection and sqldataadapter is System.Data.SqlClient:

 

SqlConnection conn = new SqlConnection("User ID=sa;password=pwd;Initial Catalog=TestDB;Data Source=servername");

SqlDataAdapter dap = new SqlDataAdapter();

DataView dv = new DataView();

 

Define a Page_Unload event, this event closes the connection and disposes the dataadapter object.

 

    void Page_Unload(object sender, EventArgs e)

    {        

        dap.Dispose();

        conn.Close();       

    }

 

Define a function that returns the dataview object. This object contains the result which is fetched from the database using sqldataadapter:

 

    public DataView GetEmployeeRecord()

    {        

        DataTable dt = new DataTable();

        dap = new SqlDataAdapter("select empfirstname, emplastname, age from employees", conn);

        dap.Fill(dt);

        dv = dt.DefaultView;       

        return dv;

    }

 

Now the last task left is binding the result to gridview control. On page load check the IsPostback property and on false bind the result (IsPostback property checks the page Postback event).

 

    protected void Page_Load(object sender, EventArgs e)

    {

        this.Page.Unload += new EventHandler(Page_Unload);

       

        if (!IsPostBack)

        {

            dv = GetEmployeeRecord();

            dv.Sort = "empfirstname";

            gr.DataSource = dv;

            gr.DataBind();

        }

    }

 

this.Page.Unload defines the Unload event. “dv.Sort” sorts the record with respect the “empfirstname” column record. You can add more then one column it works same as Order By clause usually used in select query.

Article Comments
***
tahnks

Posted on 3/18/2008 8:36:19 AM by sandip

It is very useful for beginners, add more articles for beginners

Posted on 8/25/2008 12:03:09 AM by Nithya aK

it is very advantage coding for every one
thanks

Posted on 11/21/2009 12:47:55 PM by dev

thank you very much for providing this feature for beginners, i hope you will do best examples, once agin thank u very much

Posted on 2/1/2010 11:34:45 AM by nalla raja ramesh

i want to display product details for individual customer which he buys at shop in tabular format on the form itself without storing it in DB.after that it ll b get stored in DB.
so i wanted to know if there is any control in Visual studio to do the same???????
so kindly reply for this comment..........

Posted on 2/28/2010 3:51:03 AM by arun mumbarkar

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Using String.Split to split string

Disclaimer - Privacy
© 2002-2017 DevASP.net