Saturday, March 24, 2012

Trouble with Columns

I've got a big dataset with lots of stuff in it.

I need to make another dataset containing some of the columns in the original dataset.


DataColumn dcJobCode = dsOriginal.Tables[0].Columns["JobCode"];

DataSet dsSmaller = new DataSet();
dsSmaller.Tables.Add();
dsSmaller.Tables[0].Columns.Add(dcJobCode); //this is the line that bombs

each time I do this though, using other methods as well as the one above, I'm told that
"Column 'JobCode' already belongs to another DataTable.".

I've even tried clearing and nulling the original dataset, but I still get the same message.
Help!You need to create a new instance of a DataColumn and copy the contents from the original. At the moment your simply copying the address [in memory] of the original DataColumn.
Thanks for your help, but I can't work out how to copy the data.

Do I have to set up a loop and copy each row of the column across manually? I can't see any methods of the DataColumn object that do this?

Thanks again.

0 comments:

Post a Comment