ARTICLE

Insert a Column in DataTable at Predefined Position

Posted by Sapan Singh Articles | ADO.NET in C# 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

Login to add your contents and source code to this article
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
post comment
     
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter