Search   Articles   Dev Forums   Personalize   Favorites   Member Login   ASP.Net Hosting
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Sunday, February 19, 2006

Search Directory
Trusted by over 7 million customers!
ASP.NET
VB.Net
C-Sharp
SQL Server
 
More Partners >>

GridView Control Beginners Guide

Author: DevASPTeam
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
Name:  sandip 
***
tahnks



Disclaimer - Privacy
Copyright © 2008 DevASP.net