6

I'm loading data from my database into a DataTable, and one of the columns is a date field.

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = "MySP";
    cmd.CommandType = CommandType.StoredProcedure;

    conn.Open();
    using (SqlDataReader rdr = cmd.ExecuteReader())
    {
        dt.Load(rdr);
    }
}

I'd like to format that column so that instead of containing a full date, it will be formatted like "MM/DD/YYYY".

I've tried looping through each row in the table and changing the cell for that column, but I get an error saying that the string isn't a valid DateTime object.

I tried changing the column DateType to a string, but I get an error saying I can't change the DateType after the table is filled.

How can I do this? This seems like such a simple thing, but I'm having so much trouble with it.

5
  • where and how are you using this data table? I'd say you should be formatting the date when you use it on your UI layer (for instance as data source) but not before. Commented Nov 24, 2010 at 21:34
  • I'm binding it to a GridView. The columns are dynamic, so I can't format it in Design View. The index of the column can be different as well, so it's not as easy as changing it in the RowDataBound event either. Commented Nov 24, 2010 at 21:37
  • If that's the case, then you may want to make the column type a string and format it before you insert the values. Commented Nov 24, 2010 at 21:42
  • @Aaron He's not manually inserting the values, everything is taken care of internally by Load() Commented Nov 24, 2010 at 21:50
  • I didn't see the sp. I would change the sp to return formatted strings instead of datetime values. Basically do the formatting in the sp, as Josh points out. Commented Nov 25, 2010 at 2:48

3 Answers 3

12

As a work around you could create a new column and store the formatted date there:

dt.Columns.Add("DateStr");

foreach (DataRow dr in dt.Rows)
{
    dr["DateStr"] = string.Format("{0:MM/dd/yyyy}", dr["OriginalDate"]);
}
Sign up to request clarification or add additional context in comments.

Comments

4

Change your stored procedure to cast the field to a string using CONVERT...

SELECT CONVERT(varchar, DateFieldName, 101) AS DateFieldNameFormatted

The 101 is the format code for mm/dd/yyyy (see link for other codes/formats)

Comments

2

You can't format a DateTime struct value. Don't think of it that way. It's purpose is to hold a value representing:

an instant in time, typically expressed as a date and time of day.

You can only specify the format when you convert the value of that instance to a string. Leave your DataTable schema as is. Wherever you need to format it, look at some of the formats you can use with the ToString(string) method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.