Updating rows in DataTable - why isn't this working?
I'm trying to update fields in a DataTable. The field I'm trying to edit is a date, I need to format it.
foreach (DataRow row in dt.Rows)
{
string originalRow = row["Departure Date"].ToString(); //displays "01/01/2010 12:00:00 AM"
row["Departure Date"] = DateTime.Parse(row["Departure Date"].ToString()).ToString("MM/dd/yyyy");
string newRow = row["Departure Date"].ToString(); //also displays "01/01/2010 12:00:00 AM"
}
How come this isn't getting up开发者_如何学Pythondated?
Your column is a DateTime column.
The column stores DateTime values and is unaware of formatting.
It's being stored internally as a DateTime, and you're not changing that, in fact the code above is completely redundant because your setting the value (DateTime
) as the DateTime
you've just parsed, which was a DateTime
originally. You need to store it as a string.
精彩评论