Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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#_Datatable - Fatal编程技术网

填充并初始化数据表-C#

填充并初始化数据表-C#,c#,datatable,C#,Datatable,以下是我的示例代码: DataTable table = new DataTable(); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Date", typeof(System.DateTime)); table.Rows.Add("cat", System.DateTime.Now); table.Rows.Add("dog", System.DateTime.Today); 在创建数据表之后,我正在添加列和行。

以下是我的示例代码:

DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Date", typeof(System.DateTime));
table.Rows.Add("cat", System.DateTime.Now);
table.Rows.Add("dog", System.DateTime.Today);
在创建数据表之后,我正在添加列和行。我需要在DataTable Initialize中的一段代码中完成所有这些操作,如下所示:

new DataTable()
{
   new Columns(),
   new Rows()
};
new DataTable
{
   Columns = { { "ID", typeof(int) } },
   Rows = { { 121 } }
}

有可能吗?

在类中使用静态方法

    public class Table
    {
        public static DataTable GetTable()
        {
            DataTable table = new DataTable();
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("Date", typeof(System.DateTime));
            table.Rows.Add("cat", System.DateTime.Now);
            table.Rows.Add("dog", System.DateTime.Today);
            return table;
        }
    }

我找到了解决方案,我们可以像这样初始化
数据表

new DataTable()
{
   new Columns(),
   new Rows()
};
new DataTable
{
   Columns = { { "ID", typeof(int) } },
   Rows = { { 121 } }
}

一点也不。DataTable没有可以初始化为对象初始化的列和行集合。还有其他IEnumerable可以用作内存中的数据。