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.

01.30.2012
| 3567 views |
  • submit to reddit
        Following is grid view code for aspx page.

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

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

        <Columns>

            <asp:TemplateField HeaderText="StudentName">

                <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='Student Name'></asp:Label>

                            </th>

                           

                            <th align="center" >

                                <asp:Label runat="server" ID="Label3" Text='Present'></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="lblStudentName" ></asp:Label>

                            </td>

                          

                            <td align="center" >

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

                            </td>

                        </tr>

                    </table>

                </ItemTemplate>

            </asp:TemplateField>

        </Columns>

   
    </asp:GridView>

 

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");
        }
    }