GridView Footer Values


I have the following GridView listed below along with the following code below and I'm trying to retrieve values from the DataTable to the Footer Template of my GridView. The values do retrieve to just label controls only.

<asp:GridView ID="PrintReport" runat="server" AutoGenerateColumns="False"
            CellPadding="4" EnableModelValidation="True" ForeColor="#333333"
            GridLines="None" Width="500px" EnableViewState="False" HorizontalAlign="Left" ShowFooter="true">
            <AlternatingRowStyle BackColor="White" />
                <asp:BoundField DataField="asgmt_title" HeaderText="Assignment">
                <HeaderStyle HorizontalAlign="Left" />
                <asp:BoundField DataField="asgmt_pntspossible" HeaderText="Pts. Possible">
                <HeaderStyle HorizontalAlign="Left" />
                <asp:BoundField DataField="asgmt_pntsearned" HeaderText="Pts. Earned">
                <HeaderStyle HorizontalAlign="Left" />
            <FooterStyle BackColor="#026FA7" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#026FA7" Font-Bold="True" ForeColor="White" Height="20px" HorizontalAlign="Left" />
            <PagerStyle BackColor="#F0F0F0" ForeColor="#5A5A5A" HorizontalAlign="Left" Height="20px" />
            <RowStyle BackColor="#F0F0F0" ForeColor="#5A5A5A" Height="20px" HorizontalAlign="Left" />
            <SelectedRowStyle BackColor="#F0F0F0" Font-Bold="True" ForeColor="Navy" Height="20px" HorizontalAlign="Left" />
        <br />
        <br />
        <asp:Label ID="lblPtsPossible" Text="Total Pts. Possible: " runat="server"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;<asp:Label ID="lblPtsEarned" Text="Total Pts Earned: " runat="server"></asp:Label>

protected void Page_Load(object sender, EventArgs e)
        string student_id;
        student_id = Request.QueryString["student_id"];

        string course_id;
        course_id = Request.QueryString["course_id"];

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BBGrades"].ConnectionString);

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "BB-Grades_PrintAssignments";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = conn;

        cmd.Parameters.AddWithValue("@student_id", student_id);
        cmd.Parameters.AddWithValue("@course_id", course_id);

        DataTable dt = new DataTable();

        SqlDataAdapter adp = new SqlDataAdapter();
        adp.SelectCommand = cmd;


            PrintReport.DataSource = dt;

            foreach (DataRow row in dt.Rows)
                lblPtsPossible.Text = row["fg_pntspossible"].ToString();
                lblPtsEarned.Text = row["fg_pntsearned"].ToString();

        catch (Exception ex)


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Do you need to bind the footer of the gridview? If yes then look at the below link
Alfred A.Commented:

You can use the RowDataBound event of your GridView to populate your footer.  The RowDataBound event occurs when a data row is bound to data in a GridView  control.

Inside the event, you can then inspect e.Row.RowType to determine the row type (e.g. Header, Footer, DataRow) and once it is a footer you can then access the cells e.Row.Cells in your footer or you could access your template controls (e.g. Label) by using FindControl function.
asp_net2Author Commented:
Hi Alfred1,

Is there any way that you could show me how to access the template controls (Label) controls by using the FindControl function rather than implementing the RowDataBound Event?? The reason why is because I would hate to implement the RowDataBound Event for only two values.

Thanks in advance!!
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Alfred A.Commented:

Try this,  example FindControlRecursive(PrintReport,lblPtsPossible)
public Control FindControlRecursive(Control Root, string  Id)


    if (Root.ID == Id)

        return Root;


    foreach (Control Ctl in Root.Controls)


        Control FoundCtl = FindControlRecursive(Ctl, Id);

        if (FoundCtl != null)

            return FoundCtl;



Open in new window

asp_net2Author Commented:
How do I tie this into what I currently have?
Alfred A.Commented:

This function is use to find a control inside another control.

For example if you are looking for a label control inside a gridview control, then you can use this function.


You can do this inside the RowDataBound event under e.Row.RowType = DataControlRowType.Footer

Dim lbl as Label = CType(e.Row.FindControl("lblPtsPossible"), Label)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.