Is it possible to set the listbox data source only for the list <Tuple <string, string, int >> Item1?
I have
public static class GlobalVariables { public static List<Tuple<string, string, int>> PopFile; } and I'm trying to use PopFile as a data source for listbox
listBox1.DataSource = GlobalVariables.PopFile; The problem is that it explicitly adds ([string], [string], [int]) to the list, but I want to add only the first elements of the tuples. Is it possible?
I could use
foreach (Tuple<string, string, int> i in GlobalVariables.PopFile) { listBox1.Items.Add(i.Item1); } but I would prefer .DataSource.
0
H20to2nd
source share3 answers
listBox1.DataSource = GlobalVariables.PopFile; listBox1.DisplayMember = "Item1"; listBox1.ValueMember = "Item3"; // optional https://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.datasource
+1
Slai
source shareIn the end, you cannot escape the loop and individually add your data from your tuple or create a new object to bind to the list.
Listbox just doesnβt have input for entering a tuple if you yourself do not extend the Listbox object to be able to handle it accordingly, but even then you still have to do something similar to your loop above.
0
Ryan lutz
source shareLINQ may come in handy ...
listBox1.DataSource = GlobalVariables.PopFile.Select(t => t.Item1).ToList() 0
Aron
source share