1

I have a datatable with more that 10 column of different type.I need to change the type of columns. How to change the datatype of all datetime columns in a datable/dataset.

0

2 Answers 2

2

Try This

DataTable dtCloned = dt.Clone();
//change data type of column
dtCloned.Columns[0].DataType = typeof(Int32);
//import row to cloned datatable
foreach (DataRow row in dt.Rows) 
{
    dtCloned.ImportRow(row);
}

Or Check this out
Change DataType Of DataTable COlumn

1
  • If you copy-paste code, add the source also! Jan 3, 2014 at 7:20
0

You can do this by following steps.

  1. Create new column with your new data types.
  2. Copy data from old column to new column. (but you remeber that data should compatible with new data type)
  3. Drop the old column.
  4. Rename the new column to old name. (use rename sp)

More details on this LINK.

Not the answer you're looking for? Browse other questions tagged or ask your own question.