Insert DataColumn At a particular index

T

Todd Plambeck

Is it possible to insert a datacolumn into an existing datatable at a
particular location? Example, I've got a datatable with 4 columns. I want
to add another column at index 0. I'd end up with 5 columns, columns(0)
would be my new datacolumn.

Thanks,

Todd
 
W

W.G. Ryan eMVP

There's no way I know of to have InsertAt functionality. However, In most
cases, the physical order is irrelevant. Most times I've seen this request
(and I agree, it would be nice), it's b/c they want the columns to appear in
a grid in a specific order. You can accomplish this with a
DataGridTableStyle in winforms or by adding columns, setting the
AutoGenerateColumns property to false and then just binding them to their
respective fields. you can specify in the designer where you want them to
appear, so in most instances, not having InsertAt is a slight incovenience
but you can get where you need to be. If however, this isn't a formatting
issue, and you absolutely need the ordering to be a certain way, you can
just create the columns and add them in the order you want - or, let me know
a little mroe about the problem and I'll see what I can do to help you with
hit.

Cheers

Bill
 
W

William \(Bill\) Vaughn

In a relational database, the column order is not significant. However,
there is nothing stopping you from using SELECT to order the columns in any
particular way. The SELECT can include expressions as well as renaming the
columns.
If you need to add client-side columns, you can get into the structure of
the DataGrid and reorder columns there. It's not easy, but it can be done.
And, no, I don't have an example.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
www.sqlreportingservices.net
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
C

Chad Z. Hower aka Kudzu

Todd Plambeck said:
Is it possible to insert a datacolumn into an existing datatable at a
particular location? Example, I've got a datatable with 4 columns. I
want to add another column at index 0. I'd end up with 5 columns,
columns(0) would be my new datacolumn.

This depends on your database. For most DB's, no.
 
C

Cor Ligthert

Todd,

No, however there is a simple routine to do that.

You set the columns in an array.
You remove the columns from the datatable (that is remove from collection
from the datatable not a delete as objec,t because they are referenced by
the array)
You add them again in the sequence you want.

I hope this helps,

Cor
 
S

Sahil Malik [MVP]

No it isn't possible.
It also doesn't make any sense.

But I'm curious - why do you need this functionality? Maybe there is an
alternative way to skin this cat?

- Sahil Malik [MVP]
Upcoming ADO.NET 2.0 book - http://tinyurl.com/9bync
 

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

Top