copy data columns (include the data rows)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi all,
i need to copy couple of columns from a multi columns (more the 200 columns)
dataset table into a new compact data table during run time,
one way is to copy the DataColumnCollection of the existing dataset to a new
dataset and then to remove the unneccessary columns - but this is a very
massive anf un effective work,
is there a much more friendlyway to do that?
is it posiblle to create a new data table and copy the relevant columns
(including there data rows!) to it??
thank's
stan
 
If I understand you correctly, the short answer is 'yes' but there's no way
to really do it efficiently in the sense of copying the rows into a new
table is going to double (rather, increase - it would only double if you
copied each of the rows and columns) the size of the data . If you don't
mind me asking, why do you need to copy a subset of the data if you already
have it? You can use DataTable.Select or create a dataview to get a new set
of the row's information based on a filter criteria. This still increases
the footprint but ti's a clean way to handle it. You can then just iterate
through the columns that you want and ignore the other ones. This gives you
the benefit of being able to do it more than once. Otherwise, you need two
loops, one foreach(dataColumn ) and then one nested foreach (dataRow). YOu
can use a switch statement or wahtever you like in the DataColumn foreach so
that you ignore the columns you don't want. Depending on how the table is
structured, you may be able to do this iteratively - otherwise you are going
to hard code it.

And although I'm typically loathe to make such a suggestion, depending on
your end goal, it may be a decent approach to requery the database if the
subset of data is small. Personally, I would change the initial query so
taht the columns I needed were right next to each other first, then the
other ones at the end. This would let me iterate through x number of columns
and deal with only what I wanted. This would eliminate the need create a new
structure at all or requery the database in most instances. Again though,
this is dependent on what your needs are for doing this in the first place.
If you can tell me a little more about it, I can probably be of more help.

HTH,

Bill
 
I can't think of a prepackaged way in ADO.NET to
copy over the part of the schema and create new
DataRows in tune with that partial schema copy.

You could easily write this on your own to build
the new DataTable from scratch based on the
DataColumn properties of the old DataTable.
Then, just copy the values from the old DataTable.
 
Stanley,

In my opinion is your solution one of the nicest.

You can as well think of creating a new empty datatable with the needed
columns and primary keys and than do a merge in that from your source
datatable.

http://www.windowsformsdatagridhelp.com/default.aspx?ID=edb1409d-5394-468f-a63f-de3a5d92b14a

You can do a foreach loop (We have a sample for that however that is more
properiate if you want to move the columns for external use).

However there are not much other methods. (Although I am curious why you
want this, because normally there is normaly not any need for this, except
that external use than that I mentioned already)

I hope this helps,

Cor
 
Back
Top