How to add New Column to existing DataTable in specific position.

G

GoodMorningSky

I want to add new DataColumn to existing DataTable object as first column
shown in the datagrid.
(like
DataColumn dc = new DataColumn("Target");
dc.DataType = typeof(string);
dc.DefaultValue = tbl.TableName;
tbl.Columns.Add(dc);
)
Whenever I add it, the column is shown at the end.
How can I add it as first column (or with index)?
 
K

Ken Cox [Microsoft MVP]

Does the order of the data columns really matter? You can determine the
order of the columns when you design the grid.
 
K

Ken Cox [Microsoft MVP]

Hmmm. That adds to the datagrid but he asked about the DataTable
 
A

Alex Homer

You mean you're supposed to read the question as well? <g>

I guess one way would be to build a new DataTable by adding the new column
first and then using the
DataColumnCollection.AddRange Method
to copy the others, but that seems hard work. Better to just specify the
columns in the required order in the control that displays the rows (which
is where I was going with the DataGrid...)

.... or change the query that fills the DataTable?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top