6 Replies - 627 Views - Last Post: 20 September 2017 - 11:45 PM Rate Topic: -----

#1 ikhlas06  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 178
  • Joined: 20-May 16

How do I display GrandTotal in gridview?

Posted 19 September 2017 - 12:55 PM

I am using vb asp.net

I have a grid which display the "running total on current page". but I need to display grand total.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
            DataKeyField="ID"  
            AllowSorting="True"  OnSorting="GridView1_Sorting" 
            AllowPaging="true" PageSize="3" OnPageIndexChanging="GridView1_PageIndexChanging"  
            OnRowDataBound="OnRowDataBound"
            ShowFooter="true">
                <Columns>
                    ...
                    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                    <asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="Amount" />
                </Columns>
                 <PagerStyle CssClass = "myPagerStyle" />
</asp:GridView>



Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
			' connect to database and select data to display in gridview
            BindGridData()
        End If
End Sub
	
	
  Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
        Dim GradTotalAmount As Integer = 0
        For Each r As GridViewRow In GridView1.Rows
            If r.RowType = DataControlRowType.DataRow Then
                GradTotalAmount += Convert.ToInt32(r.Cells(2).Text)
            End If
        Next

        ' Footer - display total 
        If e.Row.RowType = DataControlRowType.Footer Then
            e.Row.Cells(2).Text = GradTotalAmount.ToString()
        End If
End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: How do I display GrandTotal in gridview?

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13485
  • View blog
  • Posts: 53,843
  • Joined: 12-June 08

Re: How do I display GrandTotal in gridview?

Posted 19 September 2017 - 01:09 PM

Okay.. so put a textbox to show that.
Was This Post Helpful? 0
  • +
  • -

#3 ikhlas06  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 178
  • Joined: 20-May 16

Re: How do I display GrandTotal in gridview?

Posted 19 September 2017 - 01:15 PM

View Postmodi123_1, on 19 September 2017 - 01:09 PM, said:

Okay.. so put a textbox to show that.

becuase I need it to display in 'DataControlRowType.Footer'.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13485
  • View blog
  • Posts: 53,843
  • Joined: 12-June 08

Re: How do I display GrandTotal in gridview?

Posted 19 September 2017 - 01:23 PM

So this?
https://docs.microso...iew-s-footer-vb
Was This Post Helpful? 0
  • +
  • -

#5 ikhlas06  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 178
  • Joined: 20-May 16

Re: How do I display GrandTotal in gridview?

Posted 20 September 2017 - 08:08 AM

i used the same link to write my code. this only get you current page on gridview total not grad total.
Was This Post Helpful? 0
  • +
  • -

#6 ikhlas06  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 178
  • Joined: 20-May 16

Re: How do I display GrandTotal in gridview?

Posted 20 September 2017 - 08:44 AM

got it! turn out i get grand total by using save state. Here is a solution, incase some one else needs it.

in this example I am getting sub total as well as grand total.



<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
            DataKeyField="ID"  
            AllowSorting="True"  OnSorting="GridView1_Sorting" 
            AllowPaging="true" PageSize="3" OnPageIndexChanging="GridView1_PageIndexChanging"  
            OnRowDataBound="OnRowDataBound"
            ShowFooter="true">
                <Columns>
                    ...
                    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                    <asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="Amount" />
                </Columns>
                 <PagerStyle CssClass = "myPagerStyle" />
</asp:GridView>




Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
			' connect to database and select data to display in gridview
            BindGridData()
        End If
End Sub



Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
		' sub total 
        Dim SubTotalAmount As Integer = 0
        For Each r As GridViewRow In GridView1.Rows
            If r.RowType = DataControlRowType.DataRow Then
                SubTotalAmount += Convert.ToInt32(r.Cells(2).Text)
            End If
        Next

		' grand total
		Dim dt = TryCast(Session("SaveData"), DataTable)
        Dim GrandTotalAmount As Object = dt.Compute("SUM(Amount)", "")
		
        ' Footer - display sub total or grad total 
        If e.Row.RowType = DataControlRowType.Footer Then
            e.Row.Cells(2).Text = GradTotalAmount.ToString()
        End If
End Sub

Was This Post Helpful? 1
  • +
  • -

#7 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6376
  • View blog
  • Posts: 25,763
  • Joined: 12-December 12

Re: How do I display GrandTotal in gridview?

Posted 20 September 2017 - 11:45 PM

Thank you for posting back with your solution.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1