ARTICLE

Insert a Column in DataTable at Predefined Position

Posted by Sapan Singh Articles | ADO.NET July 26, 2012
In this article I am explaining how to add a DataColumn in a particular position in a datatable.
Reader Level:

If we want to add a column in a table at a particular position.

To do this task we use the DataTable function SetOrdinal().

For this function we use the namespace:

System.Data;

We have a DataTable with some columns:

DataTable
dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("FirstName");
dt.Columns.Add("LastName");
dt.Columns.Add("Address");
dt.Columns.Add("City");

The table structure is:

ID FirstName LastName Address City

Now we want to add a PhoneNo column after the LastName column. For this we use the SetOrdinal function, as iin:

dt.Columns.Add("PhoneNo").SetOrdinal(3);

3 is the position number and positions start from 0.

Now the table structure will be:

ID  FirstName  LastName  LastName PhoneNo Address City

Article Extensions
Contents added by chetan kumar s on Feb 25, 2013
How to read xml data to u own asp.net website,

i hope this is usefull........

regards
chetan kumar s
COMMENT USING
Employers - Post Free Jobs