Add ASP.Net DataColumn to DataTable

Updated on 20 Aug 2012,
Published on 03 Sep 2008

ASP.Net DataColumn creates a column for DataTable but how to add a new column to the DataTable? First of all you need to initialize an instance for a DataTable into which you will add a new ASP.Net DataColumn. In the previous tutorial about Using ASP.Net DataColumn properties we learnt the C# code for setting the properties of DataColumn. There we also learnt how to create an AutoIncrement Identity column with DataType as integer and other column with DataType as string. Here we will learn how to Add ASP.Net DataColumn to a DataTable.

Initializing ASP.Net DataTable Class Object

DataTable myDataTable = newDataTable(); 

Above C# code line shows how to initialize an instance for DataTable class object to create an in-memory table schema. 

C# Code to Add ASP.Net DataColumn to a DataTable

// Initialize a DataTable
DataTable myDataTable = newDataTable();

// Initialize DataColumn 
DataColumn myDataColumn = newDataColumn();

// Add First DataColumn 
// AllowDBNull property
myDataColumn.AllowDBNull = false;

// set AutoIncrement property to true
myDataColumn.AutoIncrement = true;

// set AutoIncrementSeed property equal to 1
myDataColumn.AutoIncrementSeed = 1;

// set AutoIncrementStep property equal to 1
myDataColumn.AutoIncrementStep = 1;

// set ColumnName property to specify the column name
myDataColumn.ColumnName = "auto_ID";

// set DataType property of the column as Integer
myDataColumn.DataType = System.Type.GetType("System.Int32");

// set Unique property of DataColumn to true to allow unqiue value for this column in each row
myDataColumn.Unique = true;

// Add and Create a first DataColumn
myDataTable.Columns.Add(myDataColumn);

Above C# code will add and create a new auto incrementing DataColumn to the DataTable.

// Add second DataColumn 
// initialize a new instance of DataColumn to add another column with different properties.
myDataColumn = newDataColumn(); 
myDataColumn.ColumnName = "firstName";

// set DataType property of the column as String 
myDataColumn.DataType = System.Type.GetType("System.String");

// Add and Create a Second DataColumn 
myDataTable.Columns.Add(myDataColumn);

// Add third DataColumn 
// initialize a new instance of DataColumn to add another column with different properties.
myDataColumn = newDataColumn(); 
myDataColumn.ColumnName = "lastName";

// set DataType property of the column as String 
myDataColumn.DataType = System.Type.GetType("System.String");

// Add and Create a Third DataColumn 
myDataTable.Columns.Add(myDataColumn); 

Note: ASP.Net DataTable creates the schema for DataColumn in the same order in which they are added to the table. For example in the above sample code DataTable will create DataColumn in the order of auto_ID, firstName, lastName.

Continue to next tutorial: How to Add New ASP.Net DataRow to DataTable to learn how to add data rows to datatable using C# code.

2 Responses to "Add ASP.Net DataColumn to DataTable"
Ron
Thanks for the great tutorial. How would you add columns to an existing sql table? I used your code to add columns to a data table that was filled by SQLDataAdapter. Then used the SQLDataAdapter's update function but the underlying SQL table did not change!
rajendra
actuaaly i want to grid view data binding
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email
  • HIRE EzineASP.Net Developers