Click here to Skip to main content
15,906,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 27-Oct-13 23:54pm
v2
Comments
Harshil_Raval 28-Oct-13 6:10am    
According to me that's not possible, when you add new column dynamically in datatable, it will inserted at last index. But why you want to insert new column in between Name and Address? I don't see any reason behind it.

you can use insert as shown below :

C#
DataControlField field = new AutoGeneratedField("ProductId");
field.HeaderText = "Product Id";

this.GridView2.Columns.Insert(1, field);
this.GridView2.DataBind();


above code will insert column at index 1.
 
Share this answer
 
If you want add new column at '0' Index of table then use this
----------------------------------------------------------------

DataTable dt = new DataTable();//Your DT
DataColumn col1 = new DataColumn("Profession", System.Type.GetType("System.String"));
col1.AllowDBNull = true;
col1.DefaultValue = "";
dt.Columns.Add(col1);
col1.SetOrdinal(0);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900