|
-
Aug 10th, 2012, 04:11 PM
#1
Thread Starter
Member
[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
-
Aug 10th, 2012, 04:29 PM
#2
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.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 10th, 2012, 04:32 PM
#3
Thread Starter
Member
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.
-
Aug 10th, 2012, 04:44 PM
#4
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?
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 10th, 2012, 05:09 PM
#5
Re: Getting inventory from Listview
 Originally Posted by AceInfinity
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??????
-
Aug 10th, 2012, 05:21 PM
#6
Re: Getting inventory from Listview
 Originally Posted by dunfiddlin
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:
Dim dict As New Dictionary(Of String, Integer) Using sr As New StreamReader("D:\file.txt") While sr.Peek > -1 Dim ln As String = sr.ReadLine : Dim data As String() = ln.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries) Dim name As String = data(0) Dim absVal As Integer = CInt(data(1)) Dim v As Integer = If(data(2).ToLower = "in", absVal, -absVal) If Not dict.ContainsKey(name) Then dict.Add(name, v) Else dict(name) += v End If End While End Using Dim ListItems As ListViewItem() = dict.Select(Function(i) New ListViewItem(New String() {i.Key, i.Value.ToString})).ToArray ListView1.Items.AddRange(ListItems)
Try that pietercdevries 
~Ace
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 10th, 2012, 06:06 PM
#7
Thread Starter
Member
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
-
Aug 10th, 2012, 06:45 PM
#8
Re: Getting inventory from Listview
 Originally Posted by pietercdevries
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.
 Originally Posted by pietercdevries
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.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 10th, 2012, 07:52 PM
#9
Thread Starter
Member
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.
-
Aug 10th, 2012, 08:20 PM
#10
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.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 10th, 2012, 08:27 PM
#11
Thread Starter
Member
Re: Getting inventory from Listview
This is the screen shot of the listview with the incoming and outgoing.
-
Aug 10th, 2012, 08:52 PM
#12
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?
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 10th, 2012, 08:55 PM
#13
Thread Starter
Member
Re: Getting inventory from Listview
Yes green means incoming and red means outgoing
Thanks for the help
-
Aug 10th, 2012, 09:25 PM
#14
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.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 10th, 2012, 09:35 PM
#15
Thread Starter
Member
Re: Getting inventory from Listview
-
Aug 10th, 2012, 10:13 PM
#16
Re: Getting inventory from Listview
EDIT: Slightly modified version just for testing:
vbnet Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim ListGroups As IEnumerable(Of IGrouping(Of String, ListViewItem)) _ = ListView1.Items.Cast(Of ListViewItem).GroupBy(Function(Obj) Obj.Text) Dim LVIs As ListViewItem() = (From IG As IGrouping(Of String, ListViewItem) In ListGroups Let posVal As Integer = ReturnTotal(IG, True) Let negVal As Integer = ReturnTotal(IG, False) Select New ListViewItem(New String() {IG.Key, CStr(posVal - negVal), ""})).ToArray ListView2.Items.AddRange(LVIs) End Sub Private Function ReturnTotal(IG As IGrouping(Of String, ListViewItem), ReturnPositives As Boolean) As Integer Dim ItemIsPositive As Func(Of ListViewItem, Boolean) = Function(LVI) LVI.ForeColor = Color.Green Dim ItemIsNegative As Func(Of ListViewItem, Boolean) = Function(LVI) LVI.ForeColor = Color.Red Return If(ReturnPositives, IG.Where(ItemIsPositive), IG.Where(ItemIsNegative)).Select(Function(x) Integer.Parse(x.SubItems(1).Text)).Aggregate(Function(a, b) a + b) End Function
Last edited by AceInfinity; Aug 11th, 2012 at 09:22 PM.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 11th, 2012, 07:52 AM
#17
Thread Starter
Member
Re: Getting inventory from Listview
Thank you so much
This is wondeful
-
Aug 11th, 2012, 02:09 PM
#18
Re: [RESOLVED] Getting inventory from Listview
No problem, if it works for you, or you have any questions just ask.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 11th, 2012, 02:49 PM
#19
Thread Starter
Member
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.
-
Aug 11th, 2012, 03:26 PM
#20
Re: [RESOLVED] Getting inventory from Listview
 Originally Posted by pietercdevries
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.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 11th, 2012, 06:41 PM
#21
Thread Starter
Member
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
-
Aug 11th, 2012, 07:04 PM
#22
Re: [RESOLVED] Getting inventory from Listview
 Originally Posted by pietercdevries
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.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 11th, 2012, 07:11 PM
#23
Thread Starter
Member
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
-
Aug 11th, 2012, 07:19 PM
#24
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:
If I don't know how many are in a box then how do I calculate an amount of apples?
Cheers 
~Ace
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 11th, 2012, 07:33 PM
#25
Thread Starter
Member
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
-
Aug 11th, 2012, 09:50 PM
#26
Re: [RESOLVED] Getting inventory from Listview
Yeah, here you go:
vbnet Code:
Private Enum ReturnValue Positive Negative End Enum Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim ListGroups As IEnumerable(Of IGrouping(Of String, ListViewItem)) _ = ListView1.Items.Cast(Of ListViewItem).GroupBy(Function(Obj) String.Format("{0},{1}", Obj.Text, Obj.SubItems(2).Text)) Dim LVIs As ListViewItem() = (From G As IGrouping(Of String, ListViewItem) In ListGroups Let Arr As String() = G.Key.Split(","c) Let Total As Integer = ReturnTotal(G, ReturnValue.Positive) - ReturnTotal(G, ReturnValue.Negative) Select New ListViewItem(New String() {Arr(0), CStr(Total), Arr(1)})).ToArray ListView2.Items.AddRange(LVIs) End Sub Private Function ReturnTotal(IG As IGrouping(Of String, ListViewItem), ReturnVal As ReturnValue) As Integer Dim ItemIsPositive As Func(Of ListViewItem, Boolean) = Function(LVI) LVI.ForeColor = Color.Green Dim ItemIsNegative As Func(Of ListViewItem, Boolean) = Function(LVI) LVI.ForeColor = Color.Red 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) End Function
Last edited by AceInfinity; Aug 11th, 2012 at 10:28 PM.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 11th, 2012, 10:31 PM
#27
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
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 11th, 2012, 10:44 PM
#28
Thread Starter
Member
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.
-
Aug 11th, 2012, 10:48 PM
#29
Re: [RESOLVED] Getting inventory from Listview
 Originally Posted by pietercdevries
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:
Private Enum ReturnValue Positive Negative 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:
Private Function ReturnTotal(IG As IGrouping(Of String, ListViewItem), ReturnVal As ReturnValue) As Integer Dim ItemIsPositive As Func(Of ListViewItem, Boolean) = Function(LVI) LVI.ForeColor = Color.Green Dim ItemIsNegative As Func(Of ListViewItem, Boolean) = Function(LVI) LVI.ForeColor = Color.Red 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)) Return If(intVals.Count > 1, intVals.Aggregate(Function(a, b) a + b), intVals(0)) End Function
Last edited by AceInfinity; Aug 11th, 2012 at 10:53 PM.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 11th, 2012, 10:52 PM
#30
Re: [RESOLVED] Getting inventory from Listview
You could even put it inside of it's own Sub:
vbnet Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click CalcTotals(ListView1, ListView2) End Sub Private Enum ReturnValue Positive Negative End Enum Private Sub CalcTotals(InputListView As ListView, OutputListView As ListView) Dim ListGroups As IEnumerable(Of IGrouping(Of String, ListViewItem)) _ = InputListView.Items.Cast(Of ListViewItem).GroupBy(Function(Obj) String.Format("{0},{1}", Obj.Text, Obj.SubItems(2).Text)) Dim LVIs As ListViewItem() = (From G As IGrouping(Of String, ListViewItem) In ListGroups Let Arr As String() = G.Key.Split(","c) Let Total As Integer = ReturnTotal(G, ReturnValue.Positive) - ReturnTotal(G, ReturnValue.Negative) Select New ListViewItem(New String() {Arr(0), CStr(Total), Arr(1)})).ToArray OutputListView.Items.AddRange(LVIs) End Sub Private Function ReturnTotal(IG As IGrouping(Of String, ListViewItem), ReturnVal As ReturnValue) As Integer Dim ItemIsPositive As Func(Of ListViewItem, Boolean) = Function(LVI) LVI.ForeColor = Color.Green Dim ItemIsNegative As Func(Of ListViewItem, Boolean) = Function(LVI) LVI.ForeColor = Color.Red 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)) Return If(intVals.Count > 1, intVals.Aggregate(Function(a, b) a + b), intVals(0)) End Function
Cheers 
~Ace
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 11th, 2012, 11:18 PM
#31
Thread Starter
Member
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
-
Aug 12th, 2012, 12:36 AM
#32
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
<<<------------
.NET Programming (2012 - 2018)
®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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|