且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将列添加到数据表数组中的表

更新时间:2023-11-30 22:41:46





在你的例子中,你缺少对象初始化,见下文:

Hi,

In your example you are missing object initialization, see below:
DataTable[] studentsTable = new DataTable[10];
studentsTable[0] = new  DataTable();                      // you are missing this.
studentsTable[0].Columns.Add("Name", typeof(string));
studentsTable[0].Columns.Add("ID", typeof(string));
studentsTable[0].Columns.Add("Dob", typeof(string));





一个建议是,如果所有数据表数组都有相同的列,则使用for循环添加列名。



One suggestion is that if all array of datatable have same column then use for loop to add column name.

for(int i=0;i< studentsTable.Length;i++)
{
   studentsTable[i] = new  DataTable();
   studentsTable[i].Columns.Add("Name", typeof(string));
   studentsTable[i].Columns.Add("ID", typeof(string));
   studentsTable[i].Columns.Add("Dob", typeof(string));
}



希望它有所帮助。


Hope it helps.


这是一个如何向数据表添加列的示例对象: DataTable Class [ ^ ]



如果每个数据表的列相同,我建议您创建一个数据表,然后将其添加10次到列表< T> [ ^ ]泛型类,如果没有,根据需要多次创建和添加数据表。为什么列出< T> 泛型类,而不是数组?因为它表示可以通过索引访问的强类型对象列表;提供搜索,排序和操作列表的方法。
Here is an example how to add columns to a datatable object: DataTable Class[^]

If the columns for each datatable are the same, i would suggest you to create one datatable, then to add it 10 times to List<T>[^] generic class, if not, create and add datatable as many times as you need. Why List<T> generic class, instead array? Because it represents a strongly typed list of objects that can be accessed by index; provides methods to search, sort, and manipulate lists.