DataTable.ImportRow question

K

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
 
C

Chris Jobson

kempshall said:
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
 

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