sorted list question
-
Hi all I'm totaling up a list of items in a Binding source and I am just having a small problem with syntax for incrementing the value of a key if it already exists any help is greatly appreciated line 16 below [code]SortedList mySortedList = new SortedList(); foreach (DataRowView view in myBindingSource) { if ((string)view["group_name"] == "USAT-Desktop") { view["group_name"] = "TEST TEST"; } if (mySortedList.Contains(view["category_name"])== false) { mySortedList.Add(view["category_name"], 1); } else { //how do I ++ the value of the key if it already exists? //mysortedlist[view["category_name"]].value +=1? }[/code]
-
Hi all I'm totaling up a list of items in a Binding source and I am just having a small problem with syntax for incrementing the value of a key if it already exists any help is greatly appreciated line 16 below [code]SortedList mySortedList = new SortedList(); foreach (DataRowView view in myBindingSource) { if ((string)view["group_name"] == "USAT-Desktop") { view["group_name"] = "TEST TEST"; } if (mySortedList.Contains(view["category_name"])== false) { mySortedList.Add(view["category_name"], 1); } else { //how do I ++ the value of the key if it already exists? //mysortedlist[view["category_name"]].value +=1? }[/code]
Hi,
mysortedlist[view["category_name"]] = 1+(int)mysortedlist[view["category_name"]];
should do it. BTW: the above code line was set inside <pre> </pre> tags; they work much better than [code] [/code] !
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google
-
Hi,
mysortedlist[view["category_name"]] = 1+(int)mysortedlist[view["category_name"]];
should do it. BTW: the above code line was set inside <pre> </pre> tags; they work much better than [code] [/code] !
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google
-
I'll give it a try sorry about the tags I always forget going from form to form this one is a little different
-
that works great I just tried it thanks so much I was close but I don't think I would have gotten that for a while
you're welcome.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google
-
Hi,
mysortedlist[view["category_name"]] = 1+(int)mysortedlist[view["category_name"]];
should do it. BTW: the above code line was set inside <pre> </pre> tags; they work much better than [code] [/code] !
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google
Someone else suggested this solution
private SortedList getTotals(BindingSource BS, string column)
{
SortedList list = new SortedList();
string name;foreach (DataRowView row in BS) { name = (string)row\[column\]; if (list.ContainsKey(name)) { list\[name\]++; } else { list.Add(name, 1); } } return list; }
Side thought is there an easy way to "strip" the key values into an array that i could feed a combobox?
-
Someone else suggested this solution
private SortedList getTotals(BindingSource BS, string column)
{
SortedList list = new SortedList();
string name;foreach (DataRowView row in BS) { name = (string)row\[column\]; if (list.ContainsKey(name)) { list\[name\]++; } else { list.Add(name, 1); } } return list; }
Side thought is there an easy way to "strip" the key values into an array that i could feed a combobox?
Hi, if you have .NET 2.0 or better, you can use generics such as SortedList<string,int> and that should allow you to do list[name]++; so it gives you the same base idea, a more readable source, and it eliminates the need to cast to int, so it does save some CPU cycles. I trust that is what you meant, but if so the HTML eater has swallowed it. list.Keys is the collection that holds all the key values, I guess you could feed it directly into a ComboBox through its DataSource property. If you need to know more, please read up on it either on your local MSDN that came with Visual Studio, or on http://msdn2.microsoft.com[^] :)
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google