DataTable.ImportRow question

  • Thread starter Thread starter kempshall
  • Start date Start date
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
 
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
 
Back
Top