[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

GridView Footer Values

Posted on 2010-04-06
6
Medium Priority
?
389 Views
Last Modified: 2012-05-09
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();
        }

    }
0
Comment
Question by:asp_net2
  • 3
  • 2
6 Comments
 
LVL 4

Expert Comment

by:Gagan_Jaura
ID: 29969677
Do you need to bind the footer of the gridview? If yes then look at the below link
http://aspalliance.com/782
0
 
LVL 21

Expert Comment

by:Alfred A.
ID: 29969714
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.
0
 
LVL 4

Author Comment

by:asp_net2
ID: 29970998
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!!
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 21

Expert Comment

by:Alfred A.
ID: 29971454
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

0
 
LVL 4

Author Comment

by:asp_net2
ID: 29972220
How do I tie this into what I currently have?
0
 
LVL 21

Accepted Solution

by:
Alfred A. earned 2000 total points
ID: 29972736
Hi,

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.

Or,

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

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Suggested Courses
Course of the Month8 days, 4 hours left to enroll

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question