开发者

Updating rows in DataTable - why isn't this working?

开发者 https://www.devze.com 2023-01-26 15:29 出处:网络
I\'m trying to update fields in a DataTable. The field I\'m trying to edit is a date, I need to format it.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消