Home All Groups Group Topic Archive Search About

DataTable.ImportRow question

Author
7 Jun 2006 6:02 PM
kempshall
Hi,

I have a question about the .NET DataTable.ImportRow method. If I
import a DataRow into an empty DataTable, will the empty DataTable pick
up the schema of the DataRow I'm trying to put into it? For example, I
have code that looks like this:

DataTable tableWithData;
DataTable emptyTable = new DataTable();
emptyTable.ImportRow(tableWithData.Rows[0]);

If I call tableWithData.Columns.Count, I get 5.
If I call emptyTable.Columns.Count before calling ImportRow, I get 0,
which is what I expect from the DataTable constructor with no
arguments...
But when I call emptyTable.Columns.Count after calling ImportRow, I
still get 0.
And just to make sure that a DataRow is being imported, calling
emptyTable.Rows.Count after the ImportRow statement gets me 1.

Does anybody know if there's some way that I can programmatically
import a DataTable's schema along with the data? Thanks.

--Jay

Author
7 Jun 2006 7:47 PM
Chris Jobson
Show quote
"kempshall" <jayhar***@gmail.com> wrote in message
news:1149703361.612700.34110@f6g2000cwb.googlegroups.com...
> Hi,
>
> I have a question about the .NET DataTable.ImportRow method. If I
> import a DataRow into an empty DataTable, will the empty DataTable pick
> up the schema of the DataRow I'm trying to put into it? For example, I
> have code that looks like this:
>
> DataTable tableWithData;
> DataTable emptyTable = new DataTable();
> emptyTable.ImportRow(tableWithData.Rows[0]);
>
> If I call tableWithData.Columns.Count, I get 5.
> If I call emptyTable.Columns.Count before calling ImportRow, I get 0,
> which is what I expect from the DataTable constructor with no
> arguments...
> But when I call emptyTable.Columns.Count after calling ImportRow, I
> still get 0.
> And just to make sure that a DataRow is being imported, calling
> emptyTable.Rows.Count after the ImportRow statement gets me 1.
>
> Does anybody know if there's some way that I can programmatically
> import a DataTable's schema along with the data? Thanks.

emptyTable = tableWithData.Clone(); creates an empty table with the schema
from tableWithData.

Chris Jobson

AddThis Social Bookmark Button