locked
Insert a new column in DataTable at a specified position. RRS feed

  • Question

  •  

    Iam having a datatable with 3 data columns.

    I want to insert the new data column after column 1.

    eg.I have a datatable with columns "Name","Address","State"

    I want to insert a new column "Profession" between Name and Address.

    Please let me know how to do that

    Friday, February 29, 2008 5:44 PM

Answers

  • It's no need to specify order in the DataTable since a DataTable is not visible to user, however if you display the content of the DataTable on a DataGridView control and want to order the columns, you can set the Column.DisplayIndex property instead.
    Wednesday, March 5, 2008 8:57 AM

All replies

  • It is not possible to specify the position you insert a column at.  Why does it matter the order of the columns in the data table?
    Friday, February 29, 2008 7:18 PM
  • It's no need to specify order in the DataTable since a DataTable is not visible to user, however if you display the content of the DataTable on a DataGridView control and want to order the columns, you can set the Column.DisplayIndex property instead.
    Wednesday, March 5, 2008 8:57 AM
  • You need to change that at the source of the datatable.

    For instance; If its coming from a table in a database, or a element in an XML file, you need to add the 'Profession' column there and ensure its loaded into the datatable.

    On the other hand, if its created in memory you need to find where the DataTable is being created and call the Columns.Add function - this won't allow you to insert at a specific place, you'd control that at where the data is being output.

    Hope that helps.
    Wednesday, March 5, 2008 2:33 PM
  • Please try SetOrdinal method of data column:
    DataTable.Columns.Add("ColumnName", GetType(System.String), "expression'").SetOrdinal(your index)
    • Proposed as answer by sesotek Saturday, April 18, 2009 1:03 AM
    Friday, August 29, 2008 12:55 AM
  • AoA

    Great help by Saeed.

    Regards
    Asad Naeem
    Saturday, May 16, 2009 5:02 PM
  • Its wonderful....

     

    Friday, August 27, 2010 1:29 PM
  • I know this has been answered before but I was searching everywhere and people either miss understood your question or told you a terrible work around. So for anybody else who looks up this question over 2 years on this is how you set the index of the column you wish to add to a datatable for what ever reason you may wish to do.

    dtDataTable.Columns.Add("ColumnName").SetOrdinal(1)

     

     

    Monday, November 29, 2010 10:13 AM
  • Thanks Trolog & Saeed, I did indeed look it up two years later and was very happy to find the correct answer that you provided. Good observations on the incorrect answers given. Sometimes, you jut got to say "I don't know"

     

    Thursday, December 2, 2010 9:49 PM
  • Thanks for sharing this Saeed!
    Tuesday, June 14, 2011 2:32 AM
  • Hello friends, this a solution i have used

                  
                    DataColumn col = new DataColumn("Profession", typeof(string));  
                    dt_table.Columns.Add(col);
                    dt_table.Columns["No"].SetOrdinal(1);


    • Edited by ArgeTurk Monday, December 24, 2012 12:31 PM
    Monday, December 24, 2012 12:30 PM
  • sir this is not working properly it is getting placed as last column when i tried to assign the table to datagridview 

    thank you please reply

    Wednesday, March 13, 2019 10:21 AM
  • Thanks Saeed ...
    Thursday, May 23, 2019 6:52 PM