CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2013
    Posts
    27

    Listview with multiple groups

    Hi
    I have a problem over a month now and I can’t solve it.

    I have a datagridview with 12 columns and it is bound to a datasource
    From these datagridview I create a ListView with 4 columns month, date, name and price.

    Now I want to create three groups.
    The one group under the other with totals and SubTotals and ordered by name and dates.
    I need a group of month and inside of it group of dates with subtotals and totals.

    For example I have these values to my ListView.
    Code:
    Month      Date       Name      Money
    11      20/11/2016    Abdul     12,35 €
    11      25/11/2016    Fabiano   32,85 €
    11      20/11/2016    John       9,52 €
    12      30/12/2016    Paavo     85,62 €
    12      30/12/2016    Sabas    847,22 €
    11      20/11/2016    Babs      35,47 €
    11      20/11/2016    Kaan     112,85 €
    11      25/11/2016    Ealair   512,65 €
    12      30/12/2016    Nacio     54,32 €
    11      25/11/2016    Lado      47,51 €
    And I want the result to be like that.

    Code:
    20/11/2016  Name      Money
                Abdul     12,35 €
                Babs      35,47 €
                John       9,52 €
                Kaan     112,85 €
    Sum of day           170,19 €  <-- Problem A
    25/11/2016
                Ealair   512,65 €
                Fabiano   32,85 €
                Lado      47,51 €
    Sum of day           593,01 €  <-- Problem A
    Sum Of month         763,20 €  <-- Problem B
    30/12/2016
                Nacio     54,32 €
                Paavo     85,62 €
                Sabas    847,22 €
    Sum of day           987,16 €  <-- Problem A
    Sum of month         987,16 €  <-- Problem B
    Total              1.750,36 €
    Any help. How I can do that.
    Last edited by makis_best; December 18th, 2016 at 02:21 PM.

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Listview with multiple groups

    Rather than try to do it from a datagridview I would probably just execute a query on the DB and add in the sorting needed to get the data in the order you want.

    Then it would be a simple matter of looping through the data and keeping a running total for each month adding to the listview as you go.

    Looks like your sort should be Month then Name to get the order of data you want.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Mar 2013
    Posts
    27

    Re: Listview with multiple groups

    I can't do it from SQL because user change the data inside datagrid.
    When user finish process inside datagridview (edit, delete, insert) records
    then he press a button to generate listview

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Listview with multiple groups

    Well then I would suggest that the data should be sorted into the correct order when the grid is populated

    Your sample above shows the data out of order and that would make it harder to deal with. I'm not that familiar with the data grid in VB.Net so don;t know if it has any built in sort features that you could or should make use of
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Mar 2013
    Posts
    27

    Re: Listview with multiple groups

    Hi DataMiser

    My problem is not how to short the data.
    That is something I can do.
    My problem is how to show a total under every day and a total under every month on Listview

    Thank you

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Listview with multiple groups

    If the data is sorted properly in the source then it is just a matter of looping through the data top to bottom. During the loop you need to keep track of the month and keep a running total.
    For example lets say you have a var named CurrentMonth and another called MonthTotal.

    Before you begin your loop set the CurrentMonth to the first month in your grid and the monthtotal to 0
    Within your loop you may have something like
    Code:
    If CurrentMonth=MonthFromGrid then
          MonthTotal+=AmountFromGridRow
          add currentgridrow to listview
    Else
          Add CurrentMonth and Month Total to listview as your month total line
          CurrentMonth=MonthFromCurrentGridRow
          MonthTotal=Amountfromcurrentgridrow
          Add currentGridRow to listview
    End If
    
    Move to next row
    
    repeat
    So basically what you would be doing is adding each rows amount to a running total so long as the month is the same as you add the rows to the listview
    When the month changes you write the footer line to the list view with the total of that month and reset the running total and current month to begin the running total for the new month and so on till the end of the list

    If you want to do the same for each day then you would need to add similar logic for the day as well
    Last edited by DataMiser; December 19th, 2016 at 08:34 AM.
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    Mar 2013
    Posts
    27

    Re: Listview with multiple groups

    So...

    You recommends not to use groups but simple
    insert in every line showing what ever I want to show.

    Correct?

  8. #8
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Listview with multiple groups

    Yep pretty much, Of course it could be that there has been some functionality added to the list view that I am not aware of such as some kind of grouping option but even if that were the case it might only work on the latest OS(s) and not on older ones so that too is something to consider.
    Last edited by DataMiser; December 19th, 2016 at 12:21 PM.
    Always use [code][/code] tags when posting code.

  9. #9
    Join Date
    Mar 2013
    Posts
    27

    Re: Listview with multiple groups

    Ok.

    I understand.

    I try it and I reply soon what happen.

    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured