Wednesday, November 9, 2011

How to create datatable at runtime in using

Here is the code to create a datatable at runtime in using C#.Net.

Also you can check out some posts on:

- AllowUnsafeUpdates in SharePoint 2010

- How to Create a Content Type using Visual Studio 2010 in SharePoint 2010?

- Show SharePoint list data in repeating table in infopath 2010 using SharePoint object model

Below is the namespace needed.

using System.Data;
// Create a DataTable instance
DataTable dataTable = new DataTable("DataTableName");
// Create a DataColumn instances
DataColumn dtCol1 = new DataColumn();
DataColumn dtCol2 = new DataColumn();
dtCol1.ColumnName = "Id";
dtCol1.DataType = Type.GetType("System.Int32");
dtCol2.ColumnName = "Name";
dtCol2.DataType = Type.GetType("System.String");

// Add these DataColumns into the DataTable

// Create a DataRow Instance from the table we create above, with NewRow();
DataRow row = dataTable.NewRow();

row["Id"] = 1;
row["Name"] = "Fewlines4Biju";

// Add the row into the table