Link to home
Start Free TrialLog in
Avatar of Brian
BrianFlag for United States of America

asked on

GridView Footer Values

Hello,

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" />
            <Columns>                                                                                                                                  
                <asp:BoundField DataField="asgmt_title" HeaderText="Assignment">
                <HeaderStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:BoundField DataField="asgmt_pntspossible" HeaderText="Pts. Possible">
                <HeaderStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:BoundField DataField="asgmt_pntsearned" HeaderText="Pts. Earned">
                <HeaderStyle HorizontalAlign="Left" />
                </asp:BoundField>
            </Columns>
            <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" />
        </asp:GridView>
        <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;
        adp.Fill(dt);

        try
        {
            conn.Open();

            PrintReport.DataSource = dt;
            PrintReport.DataBind();

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

        catch (Exception ex)
        {
            ex.Message.ToString();
        }

        finally
        {
            conn.Close();
        }

    }
Avatar of Gagan_Jaura
Gagan_Jaura

Do you need to bind the footer of the gridview? If yes then look at the below link
http://aspalliance.com/782
Hi,

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.
Avatar of Brian

ASKER

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!!
Hi,

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

Avatar of Brian

ASKER

How do I tie this into what I currently have?
ASKER CERTIFIED SOLUTION
Avatar of Alfred A.
Alfred A.
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial