Add strongly typed columns and rows to a memory resident data table

Data.DataTable DataTable1 = new Data.DataTable();

while (DataTable1.Columns.Contains("Record ID Column") == false) {
 Data.DataColumn NewDataColumn = new Data.DataColumn();
 NewDataColumn.ColumnName = "Record ID Column";
 NewDataColumn.DataType = System.Type.GetType("System.Int64");
 NewDataColumn.AutoIncrement = true;
 NewDataColumn.AutoIncrementSeed = 0;
 NewDataColumn.AutoIncrementStep = 1;
 DataTable1.Columns.Add(NewDataColumn);
 Data.DataColumn[] DataColumnArray = { DataTable1.Columns("Record ID Column") };
 DataTable1.PrimaryKey = DataColumnArray;
}

while (DataTable1.Columns.Contains("String Column") == false) {
 Data.DataColumn NewDataColumn = new Data.DataColumn();
 NewDataColumn.ColumnName = "String Column";
 NewDataColumn.DataType = System.Type.GetType("System.String");
 DataTable1.Columns.Add(NewDataColumn);
}

while (DataTable1.Columns.Contains("Date Column") == false) {
 Data.DataColumn NewDataColumn = new Data.DataColumn();
 NewDataColumn.ColumnName = "Date Column";
 NewDataColumn.DataType = System.Type.GetType("System.DateTime");
 DataTable1.Columns.Add(NewDataColumn);
}

while (DataTable1.Columns.Contains("Boolean Column") == false) {
 Data.DataColumn NewDataColumn = new Data.DataColumn();
 NewDataColumn.ColumnName = "Boolean Column";
 NewDataColumn.DataType = System.Type.GetType("System.Boolean");
 DataTable1.Columns.Add(NewDataColumn);
}

//Make NewDataRow reusable by wrapping it in a loop structure.
do {
 Data.DataRow NewDataRow = DataTable1.NewRow;
 NewDataRow.Item("String Column") = "Text 1";
 NewDataRow.Item("Date Column") = Today;
 NewDataRow.Item("Boolean Column") = true;
 DataTable1.Rows.Add(NewDataRow);
 break; // TODO: might not be correct. Was : Exit Do
} while (true);

do {
 Data.DataRow NewDataRow = DataTable1.NewRow;
 NewDataRow.Item("String Column") = "Text 2";
 NewDataRow.Item("Date Column") = Now;
 NewDataRow.Item("Boolean Column") = false;
 DataTable1.Rows.Add(NewDataRow);
 break; // TODO: might not be correct. Was : Exit Do
} while (true);

No comments:

Post a Comment