C
C#13mo 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
TheRanger13mo ago
listbox in winforms? wpf? web site?
jjsutton
jjsuttonOP13mo ago
Apologies, it is in winforms
TheRanger
TheRanger13mo 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
jjsuttonOP13mo ago
First one does not contain definitions for all but DataSource Second one does show my value but not the key
TheRanger
TheRanger13mo ago
ah lol just noticed its swapped, just swap it make it show the key instead
jjsutton
jjsuttonOP13mo ago
lol, now the key is showing but the value is not
TheRanger
TheRanger13mo ago
that is the point you want to display both at the same time?
jjsutton
jjsuttonOP13mo ago
Correct Ive been able to show one at a time, however been struggling to make it show both
TheRanger
TheRanger13mo ago
unsure, try removing these lines
yourlistBox.ValueMember = "Key";
yourlistBox.DisplayMember = "Value";
yourlistBox.ValueMember = "Key";
yourlistBox.DisplayMember = "Value";
jjsutton
jjsuttonOP13mo ago
Displays the value
TheRanger
TheRanger13mo ago
hmm, i recall DataSource can accept a list directly
jjsutton
jjsuttonOP13mo ago
How I originally have been doing it is through .ToList, however that only displays the value lsbScores.DataSource = scores.ToList();
TheRanger
TheRanger13mo 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
jjsuttonOP13mo ago
No description
TheRanger
TheRanger13mo ago
ok put .ToList() at the end yourlistBox.DataSource = yourSortedList.Select(x => new KeyValuePair<string,int>($"{x.Key} {x.Value}", x.Value)).ToList();
jjsutton
jjsuttonOP13mo 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
TheRanger13mo ago
well thats simple, KeyValuePair has 2 columns, Key and Value DisplayMember selects which Column to display for the user
jjsutton
jjsuttonOP13mo ago
Right, but its set to key, so why does it display both the value and key?
TheRanger
TheRanger13mo 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
jjsuttonOP13mo ago
that line is setting x to be key:value correct?
TheRanger
TheRanger13mo ago
you see, SortedList<string,int> is actually a collection of KeyValuePair<string,int>
jjsutton
jjsuttonOP13mo ago
Im not suuuper familiar with lambdas yet, so trying to understand that line specifically lol
TheRanger
TheRanger13mo ago
say u have a key called Foo with a value of 1
jjsutton
jjsuttonOP13mo ago
This I understand
TheRanger
TheRanger13mo ago
all i did was converting the key to Foo 1
jjsutton
jjsuttonOP13mo ago
Yeah, im understanding that
TheRanger
TheRanger13mo ago
so now the Key of this element is Foo 1 instead of Foo
jjsutton
jjsuttonOP13mo ago
it hasnt changed my actual key to foo 1 though right? lol
TheRanger
TheRanger13mo ago
in the sorted list itself? no
jjsutton
jjsuttonOP13mo 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
TheRanger13mo ago
yes
jjsutton
jjsuttonOP13mo ago
coool im understanding then thanks so much I appreciate it
Want results from more Discord servers?
Add your server