I have a DataSet, I am binding my GridView with this DataSet. My AutoGenerateColumns is set to true. One of the column that is returned by the DataSet has a DateTime value in it, I need only to get the date part of that and then bind the grid with the DataSet. I tried doing it this way, but that didn’t work. Any help will be appreciated.
private void FormatDataSet(DataSet ds)
{
foreach(DataRow dr in ds.Tables[0].Rows.Count)
dr["Col5"] = (DateTime.Parse(dr["col5"].ToString())).ToString("d").ToString();
}
I believe this is one of the limitations of using the AutoGenerateColumns attribute. Had you defined the columns manually via
BoundFields, you would be able to control the format with theDataFormatStringattribute.As for your case, the reason your approach doesn’t work is because you’re trying to force a string into a Date type field in the DataSet. This isn’t possible unless you change the DataSet definition.
A way around this is to go to the GridView’s RowDataBound event and edit the cell manually.