.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Server side evaluation of gridview data

Posted By:shankey       Posted Date: February 23, 2011    Points: 100    Category: ASP.NET    URL: http://www.dotnetspark.com  

Server side evaluation of gridview data. Following is the screen which evaluates the grid view data on server side
 

SERVER SIDE EVALUATION OF GRIDVIEW

Following is the screen which evaluates the grid view data on server side. Based on the data we can set the content/status of the controls. As shown below, the text of the user name column, check/uncheck of is male column, and the setting of green dot image for active user and red dot image for the inactive user are set at the server side.

Following is the structure of sample data table with UserName,Ismale,IsActive column which has string,Boolean,Boolean data respectively.

        //Creation of static datatable

        DataTable dt = new DataTable();

        dt.Columns.Add("UserName", typeof(string));

        dt.Columns.Add("IsMale", typeof(bool));

        dt.Columns.Add("IsActive", typeof(Boolean));

Following is the sample data for the above datatable which we will bind to the gridview at server side.

        dt.Rows.Add("Dharmedra", "true", "true");
        dt.Rows.Add("Katrina", "false", "true");
        dt.Rows.Add("Sunny", "true", "false");
        dt.Rows.Add("Baba ramdev", "true", "true");
        dt.Rows.Add("Sanjay dutt", "true", "false");
        dt.Rows.Add("Kareena kapoor", "false", "false");

Following is the way we bind the datatable to the grid view:

 

        //Binding static datatable to the grid view

        dgv.DataSource = dt;

        dgv.DataBind();

Now the main task we have to do i.e. Server side evaluation of the datatable binded to the grid view.

Following is the gridview we created using aspx page. The lines highlighted with yellow color are the controls used in the itemtemplate  section gridview control. These three control will be accessed at the server side and data will be evaluate at the server side.

  <asp:GridView ID="dgv" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"

        CssClass="GridViewStyle" GridLines="None" Width="100%" OnRowDataBound="dgv_RowDataBound">

        <Columns>

            <asp:TemplateField HeaderText="Name">

                <HeaderTemplate>

                    <table width="100%" border="0" cellspacing="0" cellpadding="0">

                        <tr align="center">

                            <th align="left" width="60%">

                                <asp:Label runat="server" ID="Label1" Text='User Name'>asp:Label>

                            th>

                            <th align="center" width="20%">

                                <asp:Label runat="server" ID="Label2" Text='Is Male?'>asp:Label>

                            th>

                            <th align="center" width="20%">

                                <asp:Label runat="server" ID="Label3" Text='Is Active?'>asp:Label>

                            th>

                        tr>

                    table>

                HeaderTemplate>

                <ItemTemplate>

                    <table width="100%" border="0" cellspacing="0" cellpadding="0">

                        <tr>

                            <td align="left" width="60%">

                                <asp:Label runat="server" ID="lbluser" >asp:Label>

                            td>

                            <td align="center" width="20%">

                                <asp:CheckBox runat="server" ID="chk">asp:CheckBox>

                            td>

                            <td align="center" width="20%">

                                <asp:Image runat="server" ID="imgCtrl" />

                            td>

                        tr>

                    table>

                ItemTemplate>

            asp:TemplateField>

        Columns>

        <RowStyle CssClass="RowStyle" />

        <EmptyDataRowStyle CssClass="EmptyRowStyle" />

        <PagerStyle CssClass="PagerStyle" />

        <SelectedRowStyle CssClass="SelectedRowStyle" />

        <HeaderStyle CssClass="HeaderStyle" />

        <EditRowStyle CssClass="EditRowStyle" />

        <AlternatingRowStyle CssClass="AltRowStyle" />

    asp:GridView>

 

Following is the RowDataBound event which evaluate each row of data table and will bind it to the row of the gridveiw.

 

protected void dgv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            Label lbluser = ((Label)e.Row.FindControl("lbluser"));
            CheckBox chk = ((CheckBox)e.Row.FindControl("chk"));
            Image imgCtrl = ((Image)e.Row.FindControl("imgCtrl"));

            lbluser.Text = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "UserName"));
            chk.Checked= Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "IsMale"));
            imgCtrl.ImageUrl = (Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "IsActive")) ? "~/Images/Green.png" : "~/Images/Red.png");
        }
    }

In the above section e.Row gives the current row of the gridview and FindControl() method takes control name as parameter and find and return the instance of the control. After this we can use the instance of the control to set the properties. Similar way all the control in a gridview row can be accessed and can be set as desired based on the data of the datatable binded to the gridview.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend