Move a data column in a datatable

T

Tim

Hi,

I am trying to move a datacolumn in a datatable. What I have tried so
far is to create a new datacolumn set it equal to the one I want to
remove and then remove it, add another column and add it back again in
the place I want it.

tableStyle.MappingName = "DVGlobalPrices";

System.Data.DataColumn dCol = new DataColumn();
dCol = dvData.Table.Columns["CUR_new_price_1"];

dvData.Table.Columns.Remove(dCol);

if(dvData.Table.Columns["Button"] == null)
dvData.Table.Columns.Add("Button");

dvData.Table.Columns.Add(dCol);

However the column mapping seem to be lost as the whole column is now
displayed as (null) instead of the values.

Is this the best way to go about it? Is there another way? I am trying
to add a column that is not in the original dataview. If I append the
column to the end of the columns collection everything works fine, but I
would like to put it before the last column in the dataview.

Tim
 
M

Marina

May I ask why the orders of the columns even matters?

As far as I know there is no way to move an existing column and preserve its
values.
 
T

Tim Cowan

I want to add two custom button columns at run time. They add just fine at
the end of the grid, but I want to put them next to the columns that are
associated with.

Hope you can help.

Tim

Marina said:
May I ask why the orders of the columns even matters?

As far as I know there is no way to move an existing column and preserve
its values.

Tim said:
Hi,

I am trying to move a datacolumn in a datatable. What I have tried so far
is to create a new datacolumn set it equal to the one I want to remove
and then remove it, add another column and add it back again in the place
I want it.

tableStyle.MappingName = "DVGlobalPrices";

System.Data.DataColumn dCol = new DataColumn();
dCol = dvData.Table.Columns["CUR_new_price_1"];

dvData.Table.Columns.Remove(dCol);

if(dvData.Table.Columns["Button"] == null)
dvData.Table.Columns.Add("Button");

dvData.Table.Columns.Add(dCol);

However the column mapping seem to be lost as the whole column is now
displayed as (null) instead of the values.

Is this the best way to go about it? Is there another way? I am trying to
add a column that is not in the original dataview. If I append the column
to the end of the columns collection everything works fine, but I would
like to put it before the last column in the dataview.

Tim
 
M

Marina

Ok, again, my question is, why does it matter where these columns are?

If you are displaying data in a grid you should be able to control where
what column is displayed anyway. I don't see why the actual ordering of the
columns makes a difference.

Tim Cowan said:
I want to add two custom button columns at run time. They add just fine at
the end of the grid, but I want to put them next to the columns that are
associated with.

Hope you can help.

Tim

Marina said:
May I ask why the orders of the columns even matters?

As far as I know there is no way to move an existing column and preserve
its values.

Tim said:
Hi,

I am trying to move a datacolumn in a datatable. What I have tried so
far is to create a new datacolumn set it equal to the one I want to
remove and then remove it, add another column and add it back again in
the place I want it.

tableStyle.MappingName = "DVGlobalPrices";

System.Data.DataColumn dCol = new DataColumn();
dCol = dvData.Table.Columns["CUR_new_price_1"];

dvData.Table.Columns.Remove(dCol);

if(dvData.Table.Columns["Button"] == null)
dvData.Table.Columns.Add("Button");

dvData.Table.Columns.Add(dCol);

However the column mapping seem to be lost as the whole column is now
displayed as (null) instead of the values.

Is this the best way to go about it? Is there another way? I am trying
to add a column that is not in the original dataview. If I append the
column to the end of the columns collection everything works fine, but I
would like to put it before the last column in the dataview.

Tim
 
T

Tim Cowan

Maybe, Marina, I am asking the wrong question. How do I change the display
order?

Do you know that answer to that one?

Marina said:
Ok, again, my question is, why does it matter where these columns are?

If you are displaying data in a grid you should be able to control where
what column is displayed anyway. I don't see why the actual ordering of
the columns makes a difference.

Tim Cowan said:
I want to add two custom button columns at run time. They add just fine at
the end of the grid, but I want to put them next to the columns that are
associated with.

Hope you can help.

Tim

Marina said:
May I ask why the orders of the columns even matters?

As far as I know there is no way to move an existing column and preserve
its values.

Hi,

I am trying to move a datacolumn in a datatable. What I have tried so
far is to create a new datacolumn set it equal to the one I want to
remove and then remove it, add another column and add it back again in
the place I want it.

tableStyle.MappingName = "DVGlobalPrices";

System.Data.DataColumn dCol = new DataColumn();
dCol = dvData.Table.Columns["CUR_new_price_1"];

dvData.Table.Columns.Remove(dCol);

if(dvData.Table.Columns["Button"] == null)
dvData.Table.Columns.Add("Button");

dvData.Table.Columns.Add(dCol);

However the column mapping seem to be lost as the whole column is now
displayed as (null) instead of the values.

Is this the best way to go about it? Is there another way? I am trying
to add a column that is not in the original dataview. If I append the
column to the end of the columns collection everything works fine, but
I would like to put it before the last column in the dataview.

Tim
 

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