Results 1 to 32 of 32

Thread: [RESOLVED] Getting inventory from Listview

  1. #1
    Member
    Join Date
    Mar 11
    Posts
    44

    Resolved [RESOLVED] Getting inventory from Listview

    Hi,
    I am making an vb.net 2010 application for a depot.
    The user can put what comes in and goes out the depot.
    Then you can see a listview with all the incoming and outgoing.
    What you see is this as an example.

    Item Amount In/Out

    Apple 10 in
    Apple 5 out
    Apple 3 in
    Shoes 90 in
    Shoes 5 out
    Pants 23 in
    Pants 44 in
    Pants 9 out

    Now I want to push a button to show me only this in the same listview

    Item Amount

    Apple 8
    Shoes 85
    Pants 56

    of-course the listview is never a fixed amount of items.

    Thanks for the help

  2. #2
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    585

    Re: Getting inventory from Listview

    In the same Listview? Then you'll be re-adding items.

    Where is this information coming from? How exactly do you want it to show in the ListView? The idea you're trying to portray here, or the issue at hand it still a bit unclear.
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  3. #3
    Member
    Join Date
    Mar 11
    Posts
    44

    Re: Getting inventory from Listview

    Well the information is been put in by the user and saves it to a text file. I want to show it like the example I showed.
    It makes from 2 or more the same item one and add or subtracts the amount depending if it went out or came in.

  4. #4
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    585

    Re: Getting inventory from Listview

    Ahh... I see exactly what you mean now. So the text file, looks exactly like this?
    Code:
    Apple 10 in
    Apple 5 out
    Apple 3 in 
    Shoes 90 in
    Shoes 5 out
    Pants 23 in
    Pants 44 in
    Pants 9 out
    And for the listView display, you want, Apple, Shoes and Pants.

    Apple = -5 + 3
    Shoes = 90 + -5
    Pants = 23 + 44 + -9

    Why do you have 8 then for apple? Should that not be the sum of the 5 that went out (-5) + 3 in?
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,546

    Re: Getting inventory from Listview

    Quote Originally Posted by AceInfinity View Post
    Ahh... I see exactly what you mean now. So the text file, looks exactly like this?
    Code:
    Apple 10 in
    Apple 5 out
    Apple 3 in 
    Shoes 90 in
    Shoes 5 out
    Pants 23 in
    Pants 44 in
    Pants 9 out
    And for the listView display, you want, Apple, Shoes and Pants.

    Apple = -5 + 3
    Shoes = 90 + -5
    Pants = 23 + 44 + -9

    Why do you have 8 then for apple? Should that not be the sum of the 5 that went out (-5) + 3 in?
    Er ... 10 in, 5 out, 3 more in??????

  6. #6
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    585

    Re: Getting inventory from Listview

    Quote Originally Posted by dunfiddlin View Post
    Er ... 10 in, 5 out, 3 more in??????
    Ahh, thankyou actually for catching me on that, I had a 16 hour day of work yesterday after a sleepless nigh of getting up every other hour during that early morning. My mind isn't all there today, hence why i'm at home still at this time of the day. I didn't see that last Apple entry.

    vb Code:
    1. Dim dict As New Dictionary(Of String, Integer)
    2.  
    3. Using sr As New StreamReader("D:\file.txt")
    4.     While sr.Peek > -1
    5.         Dim ln As String = sr.ReadLine : Dim data As String() = ln.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
    6.  
    7.         Dim name As String = data(0)
    8.         Dim absVal As Integer = CInt(data(1))
    9.  
    10.         Dim v As Integer = If(data(2).ToLower = "in", absVal, -absVal)
    11.  
    12.         If Not dict.ContainsKey(name) Then
    13.             dict.Add(name, v)
    14.         Else
    15.             dict(name) += v
    16.         End If
    17.     End While
    18. End Using
    19.  
    20. Dim ListItems As ListViewItem() = dict.Select(Function(i) New ListViewItem(New String() {i.Key, i.Value.ToString})).ToArray
    21. ListView1.Items.AddRange(ListItems)

    Try that pietercdevries

    ~Ace
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  7. #7
    Member
    Join Date
    Mar 11
    Posts
    44

    Re: Getting inventory from Listview

    Listen,
    This is in the listview
    Item Amount In/Out

    Apple 10 in
    Apple 5 out
    Apple 3 in
    Shoes 90 in
    Shoes 5 out
    Pants 23 in
    Pants 44 in
    Pants 9 out

    This listview is filled from a text file which has nothing to do with my question

    My question is with having
    Apple 10 in
    Apple 5 out
    Apple 3 in
    Shoes 90 in
    Shoes 5 out
    Pants 23 in
    Pants 44 in
    Pants 9 out

    in a listview how can I clear this.
    Then It makes from 2 or more the same item one and add or subtracts the amount depending if it went out or came in.

    this way we can check how many item are currently in the depot

    Thank you
    Pieter

  8. #8
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    585

    Re: Getting inventory from Listview

    Quote Originally Posted by pietercdevries View Post
    Listen,
    This is in the listview
    Item Amount In/Out

    Apple 10 in
    Apple 5 out
    Apple 3 in
    Shoes 90 in
    Shoes 5 out
    Pants 23 in
    Pants 44 in
    Pants 9 out

    This listview is filled from a text file which has nothing to do with my question
    It has more to do with your question (or perhaps more the idea behind what you're trying to do) than you think... I feel as though your tone towards me being the only member to help you thus far in this thread though is a bit insulting though for the time i've invested into replying, trying to figure out what you want to do, so that I can take more time out for you and provide sample code to show you how my (assumed) solution works for you.

    Seeing as I am the contributor trying my best to help you out though, one would think that it's in your best interest to understand where i'm coming from, before getting annoyed with me for trying to help.

    Quote Originally Posted by pietercdevries View Post
    My question is with having
    Apple 10 in
    Apple 5 out
    Apple 3 in
    Shoes 90 in
    Shoes 5 out
    Pants 23 in
    Pants 44 in
    Pants 9 out

    in a listview how can I clear this.
    Then It makes from 2 or more the same item one and add or subtracts the amount depending if it went out or came in.

    this way we can check how many item are currently in the depot

    Thank you
    Pieter
    Why I say this is because... Why would you populate a listview with the data, then use up, or "waste" more time using the listview data, parsing strings from it to clear out the listview once again, and re-add the items in the state you want them to be seen in?

    When... You could just directly read from the file (as i've done in my example), and display them in that finalized view you want them in, skipping adding all the junk to the listview the first time, which will end up being cleared out anyways as soon as you figure out the totals for each item to display in the listview?

    I can answer your question the way YOU want it answered... But I usually try to help members out in full, rather than just provide code to do something that they request which may not be the best way of doing something in the first place. And usually those assumptions, are from my sole experience, which is quite a few years of .NET programming.

    You need to point out whether you want me to just dummy up, not give you any advice and just provide you code though (for what you may think is the best solution to do what you're trying to do, regardless of whether it is or not), but that in my opinion is not really going to help you. I'll politely give you that opportunity though.

    If you do it your way, it's essentially the same thing, only instead of splitting a string and looping through lines of a text file, you're looping through ListViewItems and parsing strings from each column in that ListViewItem, probably to populate ANOTHER Listview, or some variable which supports a collection of data, because you can't change the ListView collection while you're looping through the listview at the same time. Unless you implement some odd redraw or repopulation method to the entire listview each loop through each ListviewItem, and index where you last left off somehow as you change the collection which in turn would probably change the indexing, and add data to the beginning of the ListviewItem collection containing the aggregated item:value combinations, which again would be very very inefficient and slow anyways.

    So I guess my question for you is:
    "Why are you populating the listview with:

    Code:
    Apple 10 in
    Apple 5 out
    Apple 3 in 
    Shoes 90 in
    Shoes 5 out
    Pants 23 in
    Pants 44 in
    Pants 9 out
    In the first place? If by the end of this code execution you want the listview to display:

    Code:
    Apple 8 
    Shoes 85
    Pants 56
    ???"

    I am, here to help you know. So before you block some suggestions i'd hope you'd consider why I suggest things. If you have reason for why i'm off base, then point that out! I'll be happy to change my replies to suit what you want.

    EDIT: Do you just have the listview or each in and out, for the purpose of logging what has happened? For the user to view? Or some other reason?

    ~Ace
    Last edited by AceInfinity; Aug 10th, 2012 at 07:52 PM.
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  9. #9
    Member
    Join Date
    Mar 11
    Posts
    44

    Re: Getting inventory from Listview

    Thank you so much for helping me I really appreciate it.
    The reason for me loading all the data first is because I want to give the client the posibility to see what when out or in on a certain date.
    I know It is probably better to load in what I just need.

    I will show you my code I used but in not completely working.

    Dim RawData As String = Nothing
    Dim Amount As Integer

    For I = 0 To ListView.Items.Count - 1
    For P = 0 To ListView.Items.Count - 1
    If ListView.Items.Item(I).Text = ListView.Items.Item(P).Text Then
    RawData = ListView.Items.Item(P).Text & "!" & ListView.Items.Item(P).SubItems(2).Text & "!" & ListView.Items.Item(P).SubItems(3).Text & "!" & ListView.Items.Item(P).SubItems(4).Text & "!" & ListView.Items.Item(P).SubItems(5).Text & "!" & ListView.Items.Item(P).SubItems(6).Text & "!" & ListView.Items.Item(P).SubItems(7).Text
    If ListView.Items.Item(P).ForeColor = Color.Green Then
    Amount = Amount + ListView.Items.Item(P).SubItems(1).Text
    Else
    Amount = Amount - ListView.Items.Item(P).SubItems(1).Text
    End If
    End If
    Next

    If Not RawData = Nothing And Not Counts = Nothing Then
    Dim itm As New ListViewItem
    Dim str(7) As String
    Dim Data As Object

    Data = Split(RawData, "!")

    For P = 0 To UBound(Data) - 1

    str(0) = Data(0)
    str(1) = Amount
    str(2) = Data(1)
    str(3) = Data(2)
    str(4) = Data(3)
    str(5) = Data(4)
    str(6) = Data(5)
    str(7) = Data(6)

    Next
    itm = New ListViewItem(str)
    itm.ForeColor = Color.Blue
    ListView.Items.Add(itm)
    End If
    RawData = ""
    Counts = 0
    Next

    for some reason it will only give me four items even tho there might be more to display.

    Please give me any suggestions or snippets.

  10. #10
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    585

    Re: Getting inventory from Listview

    I think you've got a bit of unnecessary looping going on in that code, and a couple other things which looked a bit odd to me. If you're compiling with .NET 3.5 or higher, you have the benefit of LINQ to optimize the speed a bit more than just regular looping, you can create a query to return and get data that we need to do what you want.

    I do appreciate your understanding though, thank you very much for that. How does your data look in the original ListView though? Even if you could screenshot that, then I can provide some code for you to test out, and i'll try to explain it as well. (I don't have the text file, but before assuming that it looks as you posted, in the file, same as the way it looks in your ListView, I think it's best to clarify.)

    I can give you an example of a LINQ method, and a non LINQ method where we only loop through the ListViewItemCollection if you want. You may even see a difference in speed, depending on how much data you really have in your first ListView that shows all the "in"s and "out"s.

    If you give me an ounce of patience I can go over all this for your learning benefit

    Cheers
    ~Ace
    Last edited by AceInfinity; Aug 10th, 2012 at 08:23 PM.
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  11. #11
    Member
    Join Date
    Mar 11
    Posts
    44

    Re: Getting inventory from Listview

    This is the screen shot of the listview with the incoming and outgoing.

    Name:  Depot X Pro Monthly Raport SchreenShot.jpg
Views: 101
Size:  273.9 KB

  12. #12
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    585

    Re: Getting inventory from Listview

    So what determines in vs. out in that ListViewItem though? Is this comparable to why you have red vs. green?
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  13. #13
    Member
    Join Date
    Mar 11
    Posts
    44

    Re: Getting inventory from Listview

    Yes green means incoming and red means outgoing
    Thanks for the help

  14. #14
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    585

    Re: Getting inventory from Listview

    So we'll need to check the ListViewItem's color properties... Hold on i'm making a quick example right now.
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  15. #15
    Member
    Join Date
    Mar 11
    Posts
    44

    Re: Getting inventory from Listview

    Ok I will be waiting

  16. #16
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    585

    Re: Getting inventory from Listview

    EDIT: Slightly modified version just for testing:
    vbnet Code:
    1. Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    2.     Dim ListGroups As IEnumerable(Of IGrouping(Of String, ListViewItem)) _
    3.         = ListView1.Items.Cast(Of ListViewItem).GroupBy(Function(Obj) Obj.Text)
    4.  
    5.     Dim LVIs As ListViewItem() = (From IG As IGrouping(Of String, ListViewItem) In ListGroups
    6.         Let posVal As Integer = ReturnTotal(IG, True)
    7.         Let negVal As Integer = ReturnTotal(IG, False)
    8.         Select New ListViewItem(New String() {IG.Key, CStr(posVal - negVal), ""})).ToArray
    9.  
    10.     ListView2.Items.AddRange(LVIs)
    11. End Sub
    12.  
    13. Private Function ReturnTotal(IG As IGrouping(Of String, ListViewItem), ReturnPositives As Boolean) As Integer
    14.     Dim ItemIsPositive As Func(Of ListViewItem, Boolean) = Function(LVI) LVI.ForeColor = Color.Green
    15.     Dim ItemIsNegative As Func(Of ListViewItem, Boolean) = Function(LVI) LVI.ForeColor = Color.Red
    16.  
    17.     Return If(ReturnPositives, IG.Where(ItemIsPositive), IG.Where(ItemIsNegative)).Select(Function(x) Integer.Parse(x.SubItems(1).Text)).Aggregate(Function(a, b) a + b)
    18. End Function
    Last edited by AceInfinity; Aug 11th, 2012 at 09:22 PM.
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  17. #17
    Member
    Join Date
    Mar 11
    Posts
    44

    Re: Getting inventory from Listview

    Thank you so much
    This is wondeful

  18. #18
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    585

    Re: [RESOLVED] Getting inventory from Listview

    No problem, if it works for you, or you have any questions just ask.
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  19. #19
    Member
    Join Date
    Mar 11
    Posts
    44

    Re: [RESOLVED] Getting inventory from Listview

    I have never worked with the LINQ library.
    Maybe you can show me what you are doing.
    Also I would like to add one of the subitems if possible.
    Thanks for the Help.

  20. #20
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    585

    Re: [RESOLVED] Getting inventory from Listview

    Quote Originally Posted by pietercdevries View Post
    I have never worked with the LINQ library.
    Maybe you can show me what you are doing.
    Also I would like to add one of the subitems if possible.
    Thanks for the Help.
    >>I have never worked with the LINQ library. Maybe you can show me what you are doing.

    Sure, which part don't you understand about the code I provided or do you want me to go explain it in full detail from what I posted previously? I don't mind, but just asking to see what you want to know.

    >>Also I would like to add one of the subitems if possible.

    All possible, but what do you mean add one of the subitems? What do you want to achieve with this new/additional subitem in the mix? (If you answer in the best detail you can provide it will prevent me from asking questions over more posts so I can get to helping you faster, because I have no idea what you'd want to do with that at this point.)

    >>Thanks for the Help.

    Glad I could help
    Last edited by AceInfinity; Aug 11th, 2012 at 03:29 PM.
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  21. #21
    Member
    Join Date
    Mar 11
    Posts
    44

    Re: [RESOLVED] Getting inventory from Listview

    I see that my questions might seem confusing this is because I am not American but dutch but I am a missionary in Haiti.
    I want to put the the subitem that goes in the column Wrapping because that way I know if the user meant one bottle or a box with many bottles.
    LINQ seems very interesting and am very willing to learn it as it looks handy. I would be nice if you could just show me the basics when working with a listview.
    I am very thankful you are willing to help me that is showing real kindness.
    Pieter

  22. #22
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    585

    Re: [RESOLVED] Getting inventory from Listview

    Quote Originally Posted by pietercdevries View Post
    I see that my questions might seem confusing this is because I am not American but dutch but I am a missionary in Haiti.
    I want to put the the subitem that goes in the column Wrapping because that way I know if the user meant one bottle or a box with many bottles.
    LINQ seems very interesting and am very willing to learn it as it looks handy. I would be nice if you could just show me the basics when working with a listview.
    I am very thankful you are willing to help me that is showing real kindness.
    Pieter
    LINQ doesn't really differ much when dealing with different controls. It's just a way to Querying data. You just need to know how to construct the queries to work with different collections, which may include user controls, or just classes built under the .NET framework which hold collections, like the IEnumerable(of T) interface, and quite a few others.

    Let me get this part straight though, so if:

    Amount = 2, and Wrapping = 2, then that means 2 * 2 which is really an Amount = 4?

    Doing this additional check for the Wrapping column will inevitably slow the process down (maybe only a couple milliseconds, or less), but I just need to clarify that.

    What needs to be done though to achieve that, is we check that SubItem's Text value for String.Empty, and if it's not an "Empty String" then we know we'll need to parse that value into an Integer, and find the product of that SubItem's Text value cast to Integer, and the Amount's Text value cast to an Integer...

    Then, next step, check the ListViewItem's ForeColor property for Color.Red, and if True, negate the value to a negative integral to aggregate with the rest of the Integers. Can get a bit tricky...

    Sad part is we can't use PLINQ here, as this isn't an independent routine, it would just slow things down, whereas in some cases it could significantly speed things up for heavy independent code.
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  23. #23
    Member
    Join Date
    Mar 11
    Posts
    44

    Re: [RESOLVED] Getting inventory from Listview

    I am sorry that 2 is standing in the wrapping culumn because I was tesiting the system quickly.
    In the column Wrapping it will show for example Box, Sac, Bottle.
    What it would be is:

    Item Amount Wrapping

    Apple 10 Box
    Apple 5 Box
    Apple 3 Box
    Rice 90 Sac
    Rice 5 Sac
    Water 23 Bottle
    Water 44 Bottle
    Water 9 Bottle

  24. #24
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    585

    Re: [RESOLVED] Getting inventory from Listview

    But before I can display how many there are, I need to know how many are in each; Box, Sack, Bottle. If you want the most accurate amount.

    From that list though, it always looks like it's going to be the same kinds of packages?

    Code:
    Apple 10 Box
    Apple 5 Box
    Apple 3 Box
    
    Rice 90 Sac
    Rice 5 Sac
    
    Water 23 Bottle
    Water 44 Bottle
    Water 9 Bottle
    Apples are always in Boxes, Rice are always in Sacks, Water is always in Bottles.

    Unless it differs:
    Code:
    Apple 10 Single
    Apple 5 Box
    Apple 3 Box
    
    Rice 90 Sac
    Rice 5 Sac
    
    Water 23 Jug
    Water 44 Bottle
    Water 9 Crate
    I don't see the purpose in doing that. It's just an extra calculation per group in my LINQ, but if you provide a note to the person on the application that each amount is per Box/Sack/Bottle/Whatever, then that's usually how manufacturing goes.

    If it does differ, then we need to know all kinds of packages that each item comes in (ALL of them), including what the amount is per that package type.

    Otherwise you're going to have to store that data someplace (user input?), and retrieve that information during the LINQ process.

    But my issue right now is:
    Code:
    Apple 5 Box
    If I don't know how many are in a box then how do I calculate an amount of apples?

    Cheers
    ~Ace
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  25. #25
    Member
    Join Date
    Mar 11
    Posts
    44

    Re: [RESOLVED] Getting inventory from Listview

    Thanks haha,
    In our warehouse/Depot we have Boxes of rice but also sacs of rice.
    Somehow I need to know how many boxes of rice we have and how many sacs of rice we have.
    Pieter

  26. #26
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    585

    Re: [RESOLVED] Getting inventory from Listview

    Yeah, here you go:
    vbnet Code:
    1. Private Enum ReturnValue
    2.     Positive
    3.     Negative
    4. End Enum
    5.  
    6. Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    7.     Dim ListGroups As IEnumerable(Of IGrouping(Of String, ListViewItem)) _
    8.      = ListView1.Items.Cast(Of ListViewItem).GroupBy(Function(Obj) String.Format("{0},{1}", Obj.Text, Obj.SubItems(2).Text))
    9.  
    10.     Dim LVIs As ListViewItem() = (From G As IGrouping(Of String, ListViewItem) In ListGroups
    11.                                  Let Arr As String() = G.Key.Split(","c)
    12.                                  Let Total As Integer = ReturnTotal(G, ReturnValue.Positive) - ReturnTotal(G, ReturnValue.Negative)
    13.                                  Select New ListViewItem(New String() {Arr(0), CStr(Total), Arr(1)})).ToArray
    14.  
    15.     ListView2.Items.AddRange(LVIs)
    16. End Sub
    17.  
    18. Private Function ReturnTotal(IG As IGrouping(Of String, ListViewItem), ReturnVal As ReturnValue) As Integer
    19.     Dim ItemIsPositive As Func(Of ListViewItem, Boolean) = Function(LVI) LVI.ForeColor = Color.Green
    20.     Dim ItemIsNegative As Func(Of ListViewItem, Boolean) = Function(LVI) LVI.ForeColor = Color.Red
    21.     Return If(ReturnVal = ReturnValue.Positive, IG.Where(ItemIsPositive), IG.Where(ItemIsNegative)).Select(Function(LVI) Integer.Parse(LVI.SubItems(1).Text)).Aggregate(Function(a, b) a + b)
    22. End Function
    Last edited by AceInfinity; Aug 11th, 2012 at 10:28 PM.
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  27. #27
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    585

    Re: [RESOLVED] Getting inventory from Listview

    The way this one works, we have a variable called ListGroups, which groups ListViewItem's based on a string which is the value of the ListViewItem Text and the ListViewItem SubItem at index 2's Text separated by a comma (","). In here our grouping based on the final string result on that concatenation is added to the Key of the IGrouping collection. (This is important later for when we parse our final ListViewItems with totals to the new ListView control, because based on that concatenated string result, we can then parse the values of the Product and Package (or "Wrapper") to the ListViewItem that should represent it's entire "category" with the total amount.)

    LVIs is going to be our variable which holds the array of ListViewItems that we add to our new ListView control using the AddRange method. The way this LINQ part works... We take IGrouping(Of String, ListViewItem)'s from the IEnumerable collection we created of them in ListGroups, and turn each of the IGrouping's into a ListViewItem in this array, where the total is the total of the amount column, and the Product name and "Wrapper" are in the third column (amount is column 2 for my tests).

    We give Arr As type String() (A string array), a value of G.Key split by the comma that we used for our GroupBy claus above (we're parsing data back from that concatenated value, so that we can later add it as a string to the ListViewItem we're creating for this specific IGrouping(Of String, ListViewItem)...

    Total holds the value, as type Integer, of our Aggregated amount values, by going through our function ReturnTotal.

    From these values, we Select a new ListViewItem for that IGrouping in the IEnumerable collection, which contains strings for Arr(0) = our product name, CStr(Total) = the totalled amount cast to a string value, Arr(1) = the "Wrapper". Then take this full IEnumerable(Of ListViewItem), and convert it to an Array, so that we can use it with the AddRange method to add them to the new ListView control.

    Hope that explains much or most of it.

    Cheers
    ~Ace
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  28. #28
    Member
    Join Date
    Mar 11
    Posts
    44

    Re: [RESOLVED] Getting inventory from Listview

    Nice code but whenever I put this in it gives me an error on all the words ReturnValue in the code
    It is not declared yet.

  29. #29
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    585

    Re: [RESOLVED] Getting inventory from Listview

    Quote Originally Posted by pietercdevries View Post
    Nice code but whenever I put this in it gives me an error on all the words ReturnValue in the code
    It is not declared yet.
    You didn't add my Enum... You forgot to add this:
    vbnet Code:
    1. Private Enum ReturnValue
    2.     Positive
    3.     Negative
    4. End Enum

    And actually, this is a much better function to be using, as Aggregate requires at least 2 elements to combine, if there's only 1 then the second doesn't exist.

    vbnet Code:
    1. Private Function ReturnTotal(IG As IGrouping(Of String, ListViewItem), ReturnVal As ReturnValue) As Integer
    2.     Dim ItemIsPositive As Func(Of ListViewItem, Boolean) = Function(LVI) LVI.ForeColor = Color.Green
    3.     Dim ItemIsNegative As Func(Of ListViewItem, Boolean) = Function(LVI) LVI.ForeColor = Color.Red
    4.  
    5.     Dim intVals As IEnumerable(Of Integer) = If(ReturnVal = ReturnValue.Positive, IG.Where(ItemIsPositive), IG.Where(ItemIsNegative)).Select(Function(LVI) Integer.Parse(LVI.SubItems(1).Text))
    6.     Return If(intVals.Count > 1, intVals.Aggregate(Function(a, b) a + b), intVals(0))
    7. End Function
    Last edited by AceInfinity; Aug 11th, 2012 at 10:53 PM.
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  30. #30
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    585

    Re: [RESOLVED] Getting inventory from Listview

    You could even put it inside of it's own Sub:
    vbnet Code:
    1. Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    2.     CalcTotals(ListView1, ListView2)
    3. End Sub
    4.  
    5. Private Enum ReturnValue
    6.     Positive
    7.     Negative
    8. End Enum
    9.  
    10. Private Sub CalcTotals(InputListView As ListView, OutputListView As ListView)
    11.     Dim ListGroups As IEnumerable(Of IGrouping(Of String, ListViewItem)) _
    12.          = InputListView.Items.Cast(Of ListViewItem).GroupBy(Function(Obj) String.Format("{0},{1}", Obj.Text, Obj.SubItems(2).Text))
    13.  
    14.     Dim LVIs As ListViewItem() = (From G As IGrouping(Of String, ListViewItem) In ListGroups
    15.                                  Let Arr As String() = G.Key.Split(","c)
    16.                                  Let Total As Integer = ReturnTotal(G, ReturnValue.Positive) - ReturnTotal(G, ReturnValue.Negative)
    17.                                  Select New ListViewItem(New String() {Arr(0), CStr(Total), Arr(1)})).ToArray
    18.  
    19.     OutputListView.Items.AddRange(LVIs)
    20. End Sub
    21.  
    22. Private Function ReturnTotal(IG As IGrouping(Of String, ListViewItem), ReturnVal As ReturnValue) As Integer
    23.     Dim ItemIsPositive As Func(Of ListViewItem, Boolean) = Function(LVI) LVI.ForeColor = Color.Green
    24.     Dim ItemIsNegative As Func(Of ListViewItem, Boolean) = Function(LVI) LVI.ForeColor = Color.Red
    25.  
    26.     Dim intVals As IEnumerable(Of Integer) = If(ReturnVal = ReturnValue.Positive, IG.Where(ItemIsPositive), IG.Where(ItemIsNegative)).Select(Function(LVI) Integer.Parse(LVI.SubItems(1).Text))
    27.     Return If(intVals.Count > 1, intVals.Aggregate(Function(a, b) a + b), intVals(0))
    28. End Function

    Cheers
    ~Ace
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  31. #31
    Member
    Join Date
    Mar 11
    Posts
    44

    Re: [RESOLVED] Getting inventory from Listview

    Thanks man You are really smart.
    This is acactly what I need.
    But I still dont understand what is happening when making the queries en stuff
    Thanks so much

  32. #32
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    585

    Re: [RESOLVED] Getting inventory from Listview

    LINQ is a fairly advanced topic. I'd suggest taking a look here: http://msdn.microsoft.com/en-us/library/bb397926.aspx
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

Posting Permissions

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