Mike B Mike B - 3 months ago 10
C# Question

Multidimensional Lists in C#

At the moment I am using one list to store one part of my data, and it's working perfectly in this format:

Item
----------------
Joe Bloggs
George Forman
Peter Pan


Now, I would like to add another line to this list, for it to work like so:

NAME EMAIL
------------------------------------------------------
Joe Bloggs joe@bloggs.com
George Forman george@formangrills.co
Peter Pan me@neverland.com


I've tried using this code to create a list within a list, and this code is used in another method in a foreach loop:

// Where List is instantiated
List<List<string>> list2d = new List<List<string>>

...

// Where DataGrid instance is given the list
dg.DataSource = list2d;
dg.DataBind();

...


// In another method, where all people add their names and emails, then are added
// to the two-dimensional list
foreach (People p in ppl.results) {
list.Add(results.name);
list.Add(results.email);
list2d.Add(list);
}


When I run this, I get this result:

Capacity Count
----------------
16 16
16 16
16 16
... ...


Where am I going wrong here. How can I get the output I desire with the code I am using right now?

Answer

Why don't you use a List<People> instead of a List<List<string>> ?

Comments