2

I would like to create a list of datatable dynamically, something like :

 DataTable[] Mydt = new DataTable();

and fill it like :

Mydt[i] = .....

Do you know if such a syntax exist in c# ?

Thanks

1 Answer 1

16

You can use following code sample,

    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;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

you should add a call to sampleDataTable.AcceptChanges() before you return the table in order to mark all rows as unchanged.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.