I am trying to make a copy of a DataTable dt1 to a new one dt2. dt1 contains columns of string and boolean types. dt2 will contains only string types.
The following is my code which works fine.
public DataTable Convert(DataTable dt1)
{
try
{
var dt2 = dt1.Clone();
foreach (DataColumn dc in dt2.Columns)
{
dc.DataType = Type.GetType("System.String");
}
foreach (DataRow row in dt1.Rows)
{
dt2.ImportRow(row);
}
foreach (DataRow dr in dt2.Rows)
{
foreach (DataColumn dc in dt2.Columns)
{
bool value;
if (bool.TryParse(dr[dc].ToString(), out value))
{
dr[dc] = "+";
}
}
}
return dt2;
}
finally
{
}
}
- 1st step: Clone of dt1 and change the columns types to string.
- 2nd step: Import the rows from dt1 to dt2
- 3rd step: Change all true values to "+"
Is there a better way to perform those steps. "better" mean clearer, simpler, less code, less steps, better performance.