2

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

 DataTable[] Mydt = new DataTable();

并像这样填写:

Mydt[i] = .....

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

谢谢

4

1 回答 1

15

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

    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;
    }
于 2012-05-01T07:16:04.120 回答