How to store data from 2 columns (from a database) in a list
List<string> _items = new List<string>();
Any help is appreciated.
you can also create an array of list
List<string> [] list= new List<String> []; list[0]=new List<string>(); list[1]=new List<string>(); list[0].add("hello"); list[1].add("world");
You create a class that will represent a row with two columns:
public class Foo { // obviously you find meaningful names of the 2 properties public string Column1 { get; set; } public string Column2 { get; set; } }
and then save to List<Foo> :
List<Foo>
List<Foo> _items = new List<Foo>(); _items.Add(new Foo { Column1 = "bar", Column2 = "baz" });
Use a tuple structure like KeyValuePair
List<KeyValuePair<string, string>> _items = new List<KeyValuePair<string, string>>(); _items.Add(new KeyValuePair<string, string>(foo, bar));
I would use a class
List<MyDataClass> _items = new List<MyDataClass>(); public class MyDataClass { public string Value1 { get; set; } public string Value2 { get; set; } }
You can either create a new class to store data, or use the built-in Tuple<> class. http://msdn.microsoft.com/en-us/library/system.tuple.aspx
Tuple<>
Also, if one of the columns contains a unique identifier of some type, you can also consider using Dictionary<> .
Dictionary<>
How to get data from a list of two new columns
List<ListTwoColumns> JobIDAndJobName = new List<ListTwoColumns>(); for (int index = 0; index < JobIDAndJobName.Count;index++) { ListTwoColumns List = JobIDAndJobName[index]; if (List.Text == this.cbJob.Text) { JobID = List.ID; } }