Cannot change DataType of a column once it has data.

  • Thread starter Thread starter ad
  • Start date Start date
A

ad

I use ado.net to fill a Excel wroksheet into a DataTable.

The data in the Excel wroksheet is digital.
After the data filled into the DataTable, the DataType of each column is set
to
Double, but I want to treat them as string, I use the code to covert them

DataTable dt = ds.Tables0];
int iColCount = dt.Columns.Count;
for (int j = 0; j < iColCount; j++)
{
DataColumn myDC = dt.Columns[j];
myDC.DataType = System.Type.GetType("System.String");
}

But it fail with message Cannot change DataType of a column once it has
data.

How can I do?
 
ad said:
I use ado.net to fill a Excel wroksheet into a DataTable.

The data in the Excel wroksheet is digital.
After the data filled into the DataTable, the DataType of each column is set
to
Double, but I want to treat them as string, I use the code to covert them

DataTable dt = ds.Tables0];
int iColCount = dt.Columns.Count;
for (int j = 0; j < iColCount; j++)
{
DataColumn myDC = dt.Columns[j];
myDC.DataType = System.Type.GetType("System.String");
}

But it fail with message Cannot change DataType of a column once it has
data.

How can I do?
-----------------------------------------------------------------------
-----------------------------------------------------------------------

Do the following in this part:
DataColumn myDC = dt.Columns[j];
myDC.DataType = System.Type.GetType("System.String");

!!! Use this instead !!!!
-----------------------------------------------------------------------

DataColumn myDC = new
DataColumn(dt.Columns[j].ColumnName,System.Type.GetType("System.String"));

-----------------------------------------------------------------------
 
Back
Top