add a new row with totals - gridview

Discussion in 'ASP .Net Datagrid Control' started by John, Jun 9, 2006.

  1. John

    John Guest

    have a gridview where I need to add totals for each grouped data,

    example
    my grid looks like this
    Sales 2006:
    BMW 100
    Lexus 75
    Mercedes 70
    Total Sales: 245

    Sales 2005:
    BMW 90
    Lexus 40
    Mercedes 10
    Total Sales: 140

    I have the formatting done for all but the Total Sales row, how can I add
    that to my gridview?
     
    John, Jun 9, 2006
    #1
    1. Advertising

  2. it's easy :)

    in one of the templates add the code

    <footerItem>
    <asp:label runat="server" id="lblTotal" text='<%= intTotal %>' />
    </footerItem>

    add
    Dim intTotal as Integer = 0
    at the begining of your script (outside Page_Load)

    you probably use a function to calculate the totals of the year, just add
    this line toyour code

    intTotal += yourBrandTotal

    If you are getting the total directly from the Database in a column named,
    for example, tTotal you problable have the code like this in your template
    item

    <asp:label runat="server" id="lblBrandTotal" Text='<%# Eval("tTotal") %>' />


    just it for this:
    <asp:label runat="server" id="lblBrandTotal" Text='<%# addTotal(
    Eval("tTotal") ) %>' />

    and ad a function called addTotal like:

    Function addTotal( myValue as integer ) as integer
    intTotal += myValue
    return myValue
    End function

    and that's it :)

    --

    Bruno Alexandre
    (a Portuguese in Københanv, Danmark)


    "John" <> escreveu na mensagem
    news:...
    > have a gridview where I need to add totals for each grouped data,
    >
    > example
    > my grid looks like this
    > Sales 2006:
    > BMW 100
    > Lexus 75
    > Mercedes 70
    > Total Sales: 245
    >
    > Sales 2005:
    > BMW 90
    > Lexus 40
    > Mercedes 10
    > Total Sales: 140
    >
    > I have the formatting done for all but the Total Sales row, how can I add
    > that to my gridview?
    >
    >
    >
    >
    >
     
    Bruno Alexandre, Jun 10, 2006
    #2
    1. Advertising

  3. John

    John Guest

    I don't want the total in the footer. I want the total for each year like
    this:
    Sales 2006:
    BMW 100
    Lexus 75
    Mercedes 70
    Total Sales: 245
    Sales 2005:
    BMW 90
    Lexus 40
    Mercedes 10
    Total Sales: 140

    I will be adding a total in the footer of all the totals, but for this I
    want the totals as above.




    "Bruno Alexandre" <> wrote in message
    news:...
    > it's easy :)
    >
    > in one of the templates add the code
    >
    > <footerItem>
    > <asp:label runat="server" id="lblTotal" text='<%= intTotal %>' />
    > </footerItem>
    >
    > add
    > Dim intTotal as Integer = 0
    > at the begining of your script (outside Page_Load)
    >
    > you probably use a function to calculate the totals of the year, just add
    > this line toyour code
    >
    > intTotal += yourBrandTotal
    >
    > If you are getting the total directly from the Database in a column named,
    > for example, tTotal you problable have the code like this in your template
    > item
    >
    > <asp:label runat="server" id="lblBrandTotal" Text='<%# Eval("tTotal") %>'
    > />
    >
    >
    > just it for this:
    > <asp:label runat="server" id="lblBrandTotal" Text='<%# addTotal(
    > Eval("tTotal") ) %>' />
    >
    > and ad a function called addTotal like:
    >
    > Function addTotal( myValue as integer ) as integer
    > intTotal += myValue
    > return myValue
    > End function
    >
    > and that's it :)
    >
    > --
    >
    > Bruno Alexandre
    > (a Portuguese in Københanv, Danmark)
    >
    >
    > "John" <> escreveu na mensagem
    > news:...
    >> have a gridview where I need to add totals for each grouped data,
    >>
    >> example
    >> my grid looks like this
    >> Sales 2006:
    >> BMW 100
    >> Lexus 75
    >> Mercedes 70
    >> Total Sales: 245
    >>
    >> Sales 2005:
    >> BMW 90
    >> Lexus 40
    >> Mercedes 10
    >> Total Sales: 140
    >>
    >> I have the formatting done for all but the Total Sales row, how can I add
    >> that to my gridview?
    >>
    >>
    >>
    >>
    >>

    >
    >
     
    John, Jun 11, 2006
    #3
  4. ok then,

    using the gridview_rowcreate you can add a row with the totals everytime
    you change the year

    or

    do that in the SQL Statment using GROUP BY Clause

    --

    Bruno Alexandre
    (a Portuguese in Københanv, Danmark)


    "John" <> escreveu na mensagem
    news:...
    >I don't want the total in the footer. I want the total for each year like
    >this:
    > Sales 2006:
    > BMW 100
    > Lexus 75
    > Mercedes 70
    > Total Sales: 245
    > Sales 2005:
    > BMW 90
    > Lexus 40
    > Mercedes 10
    > Total Sales: 140
    >
    > I will be adding a total in the footer of all the totals, but for this I
    > want the totals as above.
    >
    >
    >
    >
    > "Bruno Alexandre" <> wrote in message
    > news:...
    >> it's easy :)
    >>
    >> in one of the templates add the code
    >>
    >> <footerItem>
    >> <asp:label runat="server" id="lblTotal" text='<%= intTotal %>' />
    >> </footerItem>
    >>
    >> add
    >> Dim intTotal as Integer = 0
    >> at the begining of your script (outside Page_Load)
    >>
    >> you probably use a function to calculate the totals of the year, just add
    >> this line toyour code
    >>
    >> intTotal += yourBrandTotal
    >>
    >> If you are getting the total directly from the Database in a column
    >> named, for example, tTotal you problable have the code like this in your
    >> template item
    >>
    >> <asp:label runat="server" id="lblBrandTotal" Text='<%# Eval("tTotal") %>'
    >> />
    >>
    >>
    >> just it for this:
    >> <asp:label runat="server" id="lblBrandTotal" Text='<%# addTotal(
    >> Eval("tTotal") ) %>' />
    >>
    >> and ad a function called addTotal like:
    >>
    >> Function addTotal( myValue as integer ) as integer
    >> intTotal += myValue
    >> return myValue
    >> End function
    >>
    >> and that's it :)
    >>
    >> --
    >>
    >> Bruno Alexandre
    >> (a Portuguese in Københanv, Danmark)
    >>
    >>
    >> "John" <> escreveu na mensagem
    >> news:...
    >>> have a gridview where I need to add totals for each grouped data,
    >>>
    >>> example
    >>> my grid looks like this
    >>> Sales 2006:
    >>> BMW 100
    >>> Lexus 75
    >>> Mercedes 70
    >>> Total Sales: 245
    >>>
    >>> Sales 2005:
    >>> BMW 90
    >>> Lexus 40
    >>> Mercedes 10
    >>> Total Sales: 140
    >>>
    >>> I have the formatting done for all but the Total Sales row, how can I
    >>> add
    >>> that to my gridview?
    >>>
    >>>
    >>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    Bruno Alexandre, Jun 11, 2006
    #4
  5. John

    John Guest

    do u have a code snippet of something like this? I can't use the SQL (stored
    procedure) due to its used by other apps that don't want the data grouped or
    rolled up as I need to do on my web for
    "Bruno Alexandre" <> wrote in message
    news:...
    > ok then,
    >
    > using the gridview_rowcreate you can add a row with the totals everytime
    > you change the year
    >
    > or
    >
    > do that in the SQL Statment using GROUP BY Clause
    >
    > --
    >
    > Bruno Alexandre
    > (a Portuguese in Københanv, Danmark)
    >
    >
    > "John" <> escreveu na mensagem
    > news:...
    >>I don't want the total in the footer. I want the total for each year like
    >>this:
    >> Sales 2006:
    >> BMW 100
    >> Lexus 75
    >> Mercedes 70
    >> Total Sales: 245
    >> Sales 2005:
    >> BMW 90
    >> Lexus 40
    >> Mercedes 10
    >> Total Sales: 140
    >>
    >> I will be adding a total in the footer of all the totals, but for this I
    >> want the totals as above.
    >>
    >>
    >>
    >>
    >> "Bruno Alexandre" <> wrote in message
    >> news:...
    >>> it's easy :)
    >>>
    >>> in one of the templates add the code
    >>>
    >>> <footerItem>
    >>> <asp:label runat="server" id="lblTotal" text='<%= intTotal %>' />
    >>> </footerItem>
    >>>
    >>> add
    >>> Dim intTotal as Integer = 0
    >>> at the begining of your script (outside Page_Load)
    >>>
    >>> you probably use a function to calculate the totals of the year, just
    >>> add this line toyour code
    >>>
    >>> intTotal += yourBrandTotal
    >>>
    >>> If you are getting the total directly from the Database in a column
    >>> named, for example, tTotal you problable have the code like this in your
    >>> template item
    >>>
    >>> <asp:label runat="server" id="lblBrandTotal" Text='<%# Eval("tTotal")
    >>> %>' />
    >>>
    >>>
    >>> just it for this:
    >>> <asp:label runat="server" id="lblBrandTotal" Text='<%# addTotal(
    >>> Eval("tTotal") ) %>' />
    >>>
    >>> and ad a function called addTotal like:
    >>>
    >>> Function addTotal( myValue as integer ) as integer
    >>> intTotal += myValue
    >>> return myValue
    >>> End function
    >>>
    >>> and that's it :)
    >>>
    >>> --
    >>>
    >>> Bruno Alexandre
    >>> (a Portuguese in Københanv, Danmark)
    >>>
    >>>
    >>> "John" <> escreveu na mensagem
    >>> news:...
    >>>> have a gridview where I need to add totals for each grouped data,
    >>>>
    >>>> example
    >>>> my grid looks like this
    >>>> Sales 2006:
    >>>> BMW 100
    >>>> Lexus 75
    >>>> Mercedes 70
    >>>> Total Sales: 245
    >>>>
    >>>> Sales 2005:
    >>>> BMW 90
    >>>> Lexus 40
    >>>> Mercedes 10
    >>>> Total Sales: 140
    >>>>
    >>>> I have the formatting done for all but the Total Sales row, how can I
    >>>> add
    >>>> that to my gridview?
    >>>>
    >>>>
    >>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    John, Jun 13, 2006
    #5
  6. ok...

    (i don't have nothing, just the idea)

    1. get the data from the SP to a SQLClient.DataReader
    2. populate the GV with it
    GridView.DataSource = dr
    GridView.DataBind()
    3. set a global variable to save the years (current row and previous row)
    and the brand total
    Public currentYear as string = ""
    Public newYear as string = ""
    Public totalForYear as integer = 0
    4. in the GridView_DataBound you check if the year changed, imagine that you
    present the year in a label
    currentYear = ctype( gridview.findcontrol("myYearLabel", Label).Value
    if oldYear <> currentYear then
    the years are diferent, so Add a row here with the totals
    else
    oldYear = currentYear
    totalForYear += cInt(ctype(gridview.findcontrol("myTotalBrandLabel",
    Label).Value)
    end if
    5. to add a new row with the variable follow this article for better view of
    the idea:
    http://fredrik.nsquared2.com/viewpost.aspx?PostID=201


    with the data on SQLClient.DataReader you can add row by row manually and
    check when the year changes so you can add a new row with totals, must like
    classic ASP 3.0

    --

    Bruno Alexandre
    (a Portuguese in Københanv, Danmark)


    "John" <> escreveu na mensagem
    news:...
    > do u have a code snippet of something like this? I can't use the SQL
    > (stored procedure) due to its used by other apps that don't want the data
    > grouped or rolled up as I need to do on my web for
    > "Bruno Alexandre" <> wrote in message
    > news:...
    >> ok then,
    >>
    >> using the gridview_rowcreate you can add a row with the totals everytime
    >> you change the year
    >>
    >> or
    >>
    >> do that in the SQL Statment using GROUP BY Clause
    >>
    >> --
    >>
    >> Bruno Alexandre
    >> (a Portuguese in Københanv, Danmark)
    >>
    >>
    >> "John" <> escreveu na mensagem
    >> news:...
    >>>I don't want the total in the footer. I want the total for each year
    >>>like this:
    >>> Sales 2006:
    >>> BMW 100
    >>> Lexus 75
    >>> Mercedes 70
    >>> Total Sales: 245
    >>> Sales 2005:
    >>> BMW 90
    >>> Lexus 40
    >>> Mercedes 10
    >>> Total Sales: 140
    >>>
    >>> I will be adding a total in the footer of all the totals, but for this I
    >>> want the totals as above.
    >>>
    >>>
    >>>
    >>>
    >>> "Bruno Alexandre" <> wrote in message
    >>> news:...
    >>>> it's easy :)
    >>>>
    >>>> in one of the templates add the code
    >>>>
    >>>> <footerItem>
    >>>> <asp:label runat="server" id="lblTotal" text='<%= intTotal %>' />
    >>>> </footerItem>
    >>>>
    >>>> add
    >>>> Dim intTotal as Integer = 0
    >>>> at the begining of your script (outside Page_Load)
    >>>>
    >>>> you probably use a function to calculate the totals of the year, just
    >>>> add this line toyour code
    >>>>
    >>>> intTotal += yourBrandTotal
    >>>>
    >>>> If you are getting the total directly from the Database in a column
    >>>> named, for example, tTotal you problable have the code like this in
    >>>> your template item
    >>>>
    >>>> <asp:label runat="server" id="lblBrandTotal" Text='<%# Eval("tTotal")
    >>>> %>' />
    >>>>
    >>>>
    >>>> just it for this:
    >>>> <asp:label runat="server" id="lblBrandTotal" Text='<%# addTotal(
    >>>> Eval("tTotal") ) %>' />
    >>>>
    >>>> and ad a function called addTotal like:
    >>>>
    >>>> Function addTotal( myValue as integer ) as integer
    >>>> intTotal += myValue
    >>>> return myValue
    >>>> End function
    >>>>
    >>>> and that's it :)
    >>>>
    >>>> --
    >>>>
    >>>> Bruno Alexandre
    >>>> (a Portuguese in Københanv, Danmark)
    >>>>
    >>>>
    >>>> "John" <> escreveu na mensagem
    >>>> news:...
    >>>>> have a gridview where I need to add totals for each grouped data,
    >>>>>
    >>>>> example
    >>>>> my grid looks like this
    >>>>> Sales 2006:
    >>>>> BMW 100
    >>>>> Lexus 75
    >>>>> Mercedes 70
    >>>>> Total Sales: 245
    >>>>>
    >>>>> Sales 2005:
    >>>>> BMW 90
    >>>>> Lexus 40
    >>>>> Mercedes 10
    >>>>> Total Sales: 140
    >>>>>
    >>>>> I have the formatting done for all but the Total Sales row, how can I
    >>>>> add
    >>>>> that to my gridview?
    >>>>>
    >>>>>
    >>>>>
    >>>>>
    >>>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    Bruno Alexandre, Jun 13, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dave
    Replies:
    1
    Views:
    619
    Bruno Alexandre
    Feb 14, 2007
  2. =?Utf-8?B?Um9iZXJ0IENoYXBtYW4=?=

    Running totals in gridview (when you have many totals required)

    =?Utf-8?B?Um9iZXJ0IENoYXBtYW4=?=, Feb 15, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    882
    Alexey Smirnov
    Feb 16, 2007
  3. Mike
    Replies:
    3
    Views:
    1,572
    Mark Rae [MVP]
    Feb 21, 2008
  4. D
    Replies:
    0
    Views:
    239
  5. Walt Zydhek

    GridView Totals Row as First Row

    Walt Zydhek, Jan 15, 2007, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    930
    Alvin Bruney [MVP]
    Jan 17, 2007
Loading...

Share This Page