We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

DateTime string to ShortDate in DataBoundGrid

I have a datatable gets it''s datetimes as strings. I set my datasource of my databoundgrid to a datatable. When I load the grid, the date appears as "09/12/1998 12:00:00 AM" I want to format that column as the localized ShortDate of the computer (in this case is "09/12/1998") How can I achieve this without making the date column a drop down datepicker? Thanks, I''m really stuck.

2 Replies

Administrator August 6, 2004 03:37 PM UTC

You should set the GridBoundColumnStyleInfo.CellValueType to typeof(DateTime) and then set the GridBoundColumnStyleInfo.Format to display what you want to see. If you do not want to see the buttons, then set GridBoundColumnStyleInfo.ShowButtons = hide.

Administrator August 7, 2004 08:20 AM UTC

Here is a FormLoad that populates a GridDataBoundGrid with DateTime strings, and then formats one of the columns with the locale short date format.
private void Form1_Load(object sender, System.EventArgs e)
{
	DataTable dt = new DataTable("MyTable");
	int nCols = 4;
	int nRows = 10;
	for(int i = 0; i < nCols; i++)
	dt.Columns.Add(new DataColumn(string.Format("Col{0}", i)));
	for(int i = 0; i < nRows; ++i)
	{
		DataRow dr = dt.NewRow();
		for(int j = 0; j < nCols; j++)
			dr[j] = string.Format("{0}", DateTime.Now);
			dt.Rows.Add(dr);
	}
	this.gridDataBoundGrid1.DataSource = dt;
	GridStyleInfo style = this.gridDataBoundGrid1.Binder.InternalColumns[2].StyleInfo;
	style.CellValueType = typeof(DateTime);
	style.Format = "d"; //local short date format
}
Loader.
Live Chat Icon For mobile
Up arrow icon