C
C#7mo ago
jjsutton

How can I insert my sortedlist into a listbox?

I am new to C# and currently using a sortedlist<string, int> in order to store values that I need to display in a listbox (according to my uni, this is how I need to do it). I have tried quite a few things however I am struggling to get both the key AND value into a listbox. Would anybody be able to give me some guidance on how I can go about doing this?
32 Replies
TheRanger
TheRanger7mo ago
listbox in winforms? wpf? web site?
jjsutton
jjsutton7mo ago
Apologies, it is in winforms
TheRanger
TheRanger7mo ago
try
yourlistBox.DataSource = yourSortedList;
yourlistBox.DataTextField = "Value";
yourlistBox.DataValueField = "Key";
yourlistBox.DataBind();
yourlistBox.DataSource = yourSortedList;
yourlistBox.DataTextField = "Value";
yourlistBox.DataValueField = "Key";
yourlistBox.DataBind();
or
yourlistBox.DataSource = new BindingSource(yourSortedList, null);
yourlistBox.ValueMember = "Key";
yourlistBox.DisplayMember = "Value";
yourlistBox.DataSource = new BindingSource(yourSortedList, null);
yourlistBox.ValueMember = "Key";
yourlistBox.DisplayMember = "Value";
jjsutton
jjsutton7mo ago
First one does not contain definitions for all but DataSource Second one does show my value but not the key
TheRanger
TheRanger7mo ago
ah lol just noticed its swapped, just swap it make it show the key instead
jjsutton
jjsutton7mo ago
lol, now the key is showing but the value is not
TheRanger
TheRanger7mo ago
that is the point you want to display both at the same time?
jjsutton
jjsutton7mo ago
Correct Ive been able to show one at a time, however been struggling to make it show both
TheRanger
TheRanger7mo ago
unsure, try removing these lines
yourlistBox.ValueMember = "Key";
yourlistBox.DisplayMember = "Value";
yourlistBox.ValueMember = "Key";
yourlistBox.DisplayMember = "Value";
jjsutton
jjsutton7mo ago
Displays the value
TheRanger
TheRanger7mo ago
hmm, i recall DataSource can accept a list directly
jjsutton
jjsutton7mo ago
How I originally have been doing it is through .ToList, however that only displays the value lsbScores.DataSource = scores.ToList();
TheRanger
TheRanger7mo ago
yourlistBox.DataSource = yourSortedList.Select(x => new KeyValuePair<string,int>($"{x.Key} {x.Value}", x.Value));
yourlistBox.ValueMember = "Value";
yourlistBox.DisplayMember = "Key";
yourlistBox.DataSource = yourSortedList.Select(x => new KeyValuePair<string,int>($"{x.Key} {x.Value}", x.Value));
yourlistBox.ValueMember = "Value";
yourlistBox.DisplayMember = "Key";
jjsutton
jjsutton7mo ago
No description
TheRanger
TheRanger7mo ago
ok put .ToList() at the end yourlistBox.DataSource = yourSortedList.Select(x => new KeyValuePair<string,int>($"{x.Key} {x.Value}", x.Value)).ToList();
jjsutton
jjsutton7mo ago
This works, thank you so much for that... Ill take some time to understand why it works as well. If I have any questions ill message you if you dont mind?
TheRanger
TheRanger7mo ago
well thats simple, KeyValuePair has 2 columns, Key and Value DisplayMember selects which Column to display for the user
jjsutton
jjsutton7mo ago
Right, but its set to key, so why does it display both the value and key?
TheRanger
TheRanger7mo ago
it does not the value is actually concatenated in the key by this line yourSortedList.Select(x => new KeyValuePair<string,int>($"{x.Key} {x.Value}", x.Value));
jjsutton
jjsutton7mo ago
that line is setting x to be key:value correct?
TheRanger
TheRanger7mo ago
you see, SortedList<string,int> is actually a collection of KeyValuePair<string,int>
jjsutton
jjsutton7mo ago
Im not suuuper familiar with lambdas yet, so trying to understand that line specifically lol
TheRanger
TheRanger7mo ago
say u have a key called Foo with a value of 1
jjsutton
jjsutton7mo ago
This I understand
TheRanger
TheRanger7mo ago
all i did was converting the key to Foo 1
jjsutton
jjsutton7mo ago
Yeah, im understanding that
TheRanger
TheRanger7mo ago
so now the Key of this element is Foo 1 instead of Foo
jjsutton
jjsutton7mo ago
it hasnt changed my actual key to foo 1 though right? lol
TheRanger
TheRanger7mo ago
in the sorted list itself? no
jjsutton
jjsutton7mo ago
ok cool Ah I see, ur initializing a new keyvaluepair and setting that to be my sortedlists key+value in the key right?
TheRanger
TheRanger7mo ago
yes
jjsutton
jjsutton7mo ago
coool im understanding then thanks so much I appreciate it