If you want to convert type of all or some columns to a single type e.g., string type then use the following function:
public DataTable dataTableColsToOtherType(DataTable dt, Type type, List<string> colsForTypeChange = default(List<string>))
{
var dt2 = new DataTable();
foreach (DataColumn c in dt.Columns)
{
if (colsForTypeChange != null && colsForTypeChange.Count > 0)
{
if (colsForTypeChange.Contains(c.ColumnName))
dt2.Columns.Add(c.ColumnName, type);
else dt2.Columns.Add(c.ColumnName, c.DataType);
}
else
{
dt2.Columns.Add(c.ColumnName, type);
}
}
dt2.Load(dt.CreateDataReader(), System.Data.LoadOption.OverwriteChanges);
return dt2;
}
to change all columns type call it as:
DataTable dtNew = dataTableColsToOtherType(dtOld, typeof(string));
to change specific columns type call it as:
DataTable dtNew = dataTableColsToOtherType(dtOld, typeof(string), new List<string>() { "INT_COLUMN_1","INT_COLUMN_2" });
Note:
Make sure for your desired Column Type and its data type while changing columns to a single type, otherwise you may get exceptions.