Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将两列存储到列表中_C#_Visual Studio 2010_List_C# 4.0_Multidimensional Array - Fatal编程技术网

C# 将两列存储到列表中

C# 将两列存储到列表中,c#,visual-studio-2010,list,c#-4.0,multidimensional-array,C#,Visual Studio 2010,List,C# 4.0,Multidimensional Array,如何在列表中存储来自2列(来自数据库)的数据 List<string> _items = new List<string>(); List_items=newlist(); 感谢您的帮助您创建了一个类,该类将用两列表示一行: public class Foo { // obviously you find meaningful names of the 2 properties public string Column1 { get; set; }

如何在列表中存储来自2列(来自数据库)的数据

List<string> _items = new List<string>();
List_items=newlist();

感谢您的帮助

您创建了一个类,该类将用两列表示一行:

public class Foo
{
    // obviously you find meaningful names of the 2 properties

    public string Column1 { get; set; } 
    public string Column2 { get; set; }
}
然后将其存储在
列表中

List_items=newlist();
_添加(新的Foo{Column1=“bar”,Column2=“baz”});

您可以创建一个新类来保存数据,也可以使用内置的
元组

如果其中一个列包含某种类型的唯一ID,您也可以考虑使用<代码>字典< /> > 我会使用一个类

 List<MyDataClass> _items = new List<MyDataClass>();

 public class MyDataClass
 {
     public string Value1 { get; set; }
     public string Value2 { get; set; }
 }
List_items=newlist();
公共类MyDataClass
{
公共字符串值1{get;set;}
公共字符串值2{get;set;}
}
使用元组结构,如

List_items=newlist();
_添加(新的KeyValuePair(foo,bar));

这是关于如何从新的两列列表中检索数据

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;
                }
            }
List JobIDAndJobName=new List();
for(int index=0;index
您还可以创建列表数组

List<string> [] list= new List<String> [];
list[0]=new List<string>();
list[1]=new List<string>();

list[0].add("hello");
list[1].add("world");
List[]List=新列表[];
列表[0]=新列表();
列表[1]=新列表();
列表[0]。添加(“你好”);
列表[1]。添加(“世界”);
您可以这样做:

List<IList<string>> cols = new List<IList<string>>();
List cols=new List();
您可以设置所需的列数

cols.Add(new List<string> { "", "", "","more","more","more","more","..." });
cols.Add(新列表{“”、“”、“”、“、”更多“、”更多“、”更多“、”更多“、”更多“…”);

您能告诉我如何使用您的类获取/设置数据吗。到目前为止,这看起来是一个很好的开始。我如何从我的新类中获取数据?@CocoaDev,这将在很大程度上取决于您的数据如何存储在数据库中,您使用的是什么数据库访问技术,您是否使用ORM工具。。。这里有无数的可能性。如果不知道从哪里开始,ADO.NET是.NET中事实上的数据库访问技术,每个从事关系数据库编程的开发人员在学习关系数据访问时都应该从该技术开始。我从Excel电子表格中获取数据,然后将其存储到列表中(您向我展示的示例)。所以基本上在这一点上。我的数据保存在一个名为_items3@CocoaDev,好的,那么你的问题是什么?您是如何从这个Excel电子表格中检索数据的,还是您正在询问的内容?@CocoaDev,您可以将列表框的
DataTextField
DataValueField
属性(假设您谈论的是ASP.NET列表框控件)设置为相应的属性名称。例如:
ListBox1.DataTextField=“Column1”。如果您正在进行WinForm开发,您要查找的两个属性称为
DisplayMember
ValueMember
。字典?列表?我不明白为什么这是答案,那么问题是关于列表,而不是列表的数组。
List<IList<string>> cols = new List<IList<string>>();
cols.Add(new List<string> { "", "", "","more","more","more","more","..." });