Steven
Steven

Reputation: 18869

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 updated?

Upvotes: 1

Views: 1452

Answers (2)

Matthew Abbott
Matthew Abbott

Reputation: 61599

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.

Upvotes: 2

SLaks
SLaks

Reputation: 887867

Your column is a DateTime column.

The column stores DateTime values and is unaware of formatting.

Upvotes: 3

Related Questions