4 Replies - 2947 Views - Last Post: 13 December 2012 - 07:12 AM Rate Topic: -----

#1 adrianmcg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 06-December 12

Total Gridview footer ASP.Net C#

Posted 11 December 2012 - 11:01 AM

I am trying to get a total in the footer of gridview. It was there before but dissappeared when I added some buttons to the table.

Here is my code:
    <asp:GridView ID="gvBasket" runat="server" AutoGenerateColumns="False" 
        ShowFooter="True">
        <FooterStyle Font-Bold="true" BackColor="#61A6F8" ForeColor="black" />
     <Columns>
            <asp:BoundField DataField="ProductName" HeaderText="Product Name" SortExpression="ProductName" />
            <asp:BoundField DataField="qty" HeaderText="Quantity" SortExpression="qty" />
            <asp:BoundField DataField="UnitPrice" HeaderText="Unit Prce" SortExpression="unitPrice"
                HtmlEncode="false" DataFormatString="{0:c}" />
            <asp:TemplateField HeaderText="Total" SortExpression="Total">
                <ItemTemplate>
                    <asp:Label ID="lblTotal" runat="server" Text='<%# Eval("Total", "{0:c}") %>'  />
                </ItemTemplate>
                <FooterTemplate>
                <asp:Label ID="lblsubTotal" runat="server" />

                </FooterTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="sessionID" HeaderText="Session" 
                SortExpression="sessionID" visible="false"/>
            <asp:BoundField DataField="UserID" HeaderText="user" SortExpression="UserID" Visible="false" />
            <asp:BoundField DataField="ProductID" HeaderText="ProductID" 
                SortExpression="ProductID" visible="false"/>
            <asp:TemplateField HeaderText="Edit">
                <ItemTemplate>
                    <asp:Button ID="Delete" runat="server" 
                        CommandArgument='<%# Bind("productID") %>' onclick="Delete_Click" 
                        Text="Delete" CommandName="DeleteItem" />
                    <asp:Button ID="btnAdd" runat="server" 
                        CommandArgument='<%# Bind("productID") %>' CommandName="AddItem" 
                        onclick="btnAdd_Click" Text="Add" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>



and code behind is:

        decimal stotal = 0; 
        protected void gvBasket_RowDataBound(object sender, GridViewRowEventArgs e)  {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblTotal = (Label)e.Row.FindControl("lblTotal");
                decimal Total = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Total"));
                lblTotal.Text = Total.ToString("c");
                sTotal += Total;
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            { Label lblsubTotal = (Label)e.Row.FindControl("lblsubTotal");
                lblsubTotal.Text = sTotal.ToString("c");
            }}



I want the total Total column to have a subtotal at the bottom.

my code for the table is:
                string session = Session.SessionID.ToString();
                string Query = "SELECT  tblProduct.ProductName as ProductName, tblProduct.UnitPrice as UnitPrice, sum(ShoppingBasket.qty) as Qty , tblProduct.UnitPrice*sum(ShoppingBasket.qty) as Total, ShoppingBasket.sessionID, ShoppingBasket.userID, ShoppingBasket.productID FROM ShoppingBasket join tblProduct on ShoppingBasket.productID=tblProduct.ProductID where ShoppingBasket.sessionID= '" + session + "' AND ShoppingBasket.UserID='' group by ShoppingBasket.userID, tblProduct.ProductName, tblProduct.UnitPrice, ShoppingBasket.sessionID, ShoppingBasket.productID";
               
                ConnectionHandler objHandler = new ConnectionHandler();
                DataTable dt = objHandler.ExecuteSelect(Query);


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




I have code in a DAL if this is needed please ask.

Is This A Good Question/Topic? 0
  • +

Replies To: Total Gridview footer ASP.Net C#

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: Total Gridview footer ASP.Net C#

Posted 11 December 2012 - 05:44 PM

a couple of suggestions:
first, this
lblTotal.Text = Total.ToString("c");

should not be needed since you are binding the label inside the gridview already.

you also should be able to just do
Label lblTotal = (Label)e.Row.FindControl("lblTotal");
sTotal += Convert.ToDecimal(lblTotal.Text);



or
sTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Total"));



As for your problem, when you debug it is sTotal being assigned a value?

This post has been edited by Nakor: 11 December 2012 - 05:44 PM

Was This Post Helpful? 0
  • +
  • -

#3 adrianmcg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 06-December 12

Re: Total Gridview footer ASP.Net C#

Posted 12 December 2012 - 02:47 AM

Quote

As for your problem, when you debug it is sTotal being assigned a value?


Yes I want to assign sTotal the value of the total of the column "Total" which will be displayed on label "lblTotal".

I found that the gridview did not have rowdatabound, so I assigned it to my code.

And have debugged it and am getting a nullReferenceError which was handled by user.
Was This Post Helpful? 0
  • +
  • -

#4 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: Total Gridview footer ASP.Net C#

Posted 12 December 2012 - 06:22 PM

Where are you getting the null reference at?
Was This Post Helpful? 0
  • +
  • -

#5 adrianmcg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 06-December 12

Re: Total Gridview footer ASP.Net C#

Posted 13 December 2012 - 07:12 AM

View PostNakor, on 12 December 2012 - 06:22 PM, said:

Where are you getting the null reference at?


I have sorted it. I was referencing the wrong part in the second if statement.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1