DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Server Side Evaluation Of Grid View Row.

  • submit to reddit
        Following is grid view code for aspx page.

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False"

        Width="100%" OnRowDataBound="gv_RowDataBound">


            <asp:TemplateField HeaderText="StudentName">


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

                        <tr align="center">

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

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



                            <th align="center" >

                                <asp:Label runat="server" ID="Label3" Text='Present'></asp:Label>






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


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

                                <asp:Label runat="server" ID="lblStudentName" ></asp:Label>



                            <td align="center" >

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









Following code evaluates each row of grid: 

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

            Label lblStudentName = ((Label)e.Row.FindControl("lblStudentName"));
            Image imgControl = ((Image)e.Row.FindControl("imgControl"));

            lblStudentName.Text = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "StudentName"));
            imgControl.ImageUrl = (Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "Present")) ? "Images/Checkmark.png" : "Images/Cross.png");