I am getting query results in DataTable and need to format ints and dates in DataTable to a specific format. I can not format it in SQL server due to some limitations.
I have written below code to format it but it is not updating the DataTable.
public DataTable formatDT(DataTable DT)
{
string datatype, columnName;
decimal sysInt;
DateTime DateTime;
int r = 0;
foreach (DataRow dr in DT.Rows)
{
for (int i = 0; i < DT.Columns.Count; i++)
{
columnName = DT.Columns[i].ColumnName;
if (DT.Columns[i].ColumnName.ToString() == "full_count")
{
continue;
}
datatype = Convert.ToString(DT.Columns[i].DataType);
switch (datatype)
{
case "System.Int":
case "System.Int32":
case "System.Int64":
sysInt = Convert.ToInt32(dr[columnName]);
DT.Rows[r][columnName] = sysInt.ToString("N0");
break;
case "System.DateTime":
DateTime = Convert.ToDateTime(dr[columnName]);
DT.Rows[r][columnName] = DateTime.ToString("yyyy-MM-dd");
logger.Error(DT.Rows[r][columnName] + "<<<>>>>" + DateTime.ToString("yyyy-MM-dd")); //Even after assing 'DT.Rows[r][columnName]' is shows old values and not the formatted one
break;
case "System.Decimal":
case "System.Double":
sysInt = Convert.ToInt32(dr[columnName]);
DT.Rows[r][columnName] = sysInt.ToString("N2");
break;
default:
DT.Rows[r][columnName] = dr[columnName].ToString();
break;
}
}
r++;
}
DT.AcceptChanges();
return DT;
}
Any help appreciated.