在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
我已经使用此代码到一个列表中创建一个列表试过,这个代码是用另一种方法在foreach循环:
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?
你为什么不使用列表<人>
,而不是列表<名单<串>>
Why don't you use a List<People>
instead of a List<List<string>>
?