我想动态创建一个数据表列表,例如:

 DataTable[] Mydt = new DataTable();


并像这样填充它:

Mydt[i] = .....


您知道c#中是否存在这样的语法吗?

谢谢

最佳答案

您可以使用以下代码示例,

    private static DataSet SampleData()
    {
        DataSet sampleDataSet = new DataSet();
        sampleDataSet.Locale = CultureInfo.InvariantCulture;
        DataTable sampleDataTable = sampleDataSet.Tables.Add("SampleData");

        sampleDataTable.Columns.Add("FirstColumn", typeof(string));
        sampleDataTable.Columns.Add("SecondColumn", typeof(string));
        DataRow sampleDataRow;
        for (int i = 1; i <= 49; i++)
        {
            sampleDataRow = sampleDataTable.NewRow();
            sampleDataRow["FirstColumn"] = "Cell1: " + i.ToString(CultureInfo.CurrentCulture);
            sampleDataRow["SecondColumn"] = "Cell2: " + i.ToString(CultureInfo.CurrentCulture);
            sampleDataTable.Rows.Add(sampleDataRow);
        }

        return sampleDataSet;
    }

关于c# - 如何创建动态数据表?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10394680/

10-12 16:32