2008/10/22

Tips: Converting DataTable's DataType

Here is the way to convert a DataTable's DataType from whatever types to the String type.
private static DataTable ConvertTableType(DataTable dt)
{
  DataTable newDt = dt.Clone();

  //convert all columns' datatype
  foreach (DataColumn dc in newDt.Columns)
  {
    dc.DataType = Type.GetType("System.String");
  }

  //import data from original table
  foreach (DataRow dr in dt.Rows)
  {
    newDt.ImportRow(dr);
  }
  dt.Dispose();

  return newDt;
}

1 comment: