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

Changing Date format in GDBG generates an edition error

Hi I have a GBDB bound to a DataTable containing dates. I want to change the date format (year displayed with 2 digits) whitout modifying the regional settings. I have tried to change the format of the grid model, but it haves no effect. grid.Model.ColStyles[colName].Format="dd/MM/yy" So, I have overriden PrepareViewStyleInfo to change the format and it works fine. protected override void OnPrepareViewStyleInfo(GridPrepareViewStyleInfoEventArgs e) { if(e.Style.CellValueType == typeof(System.DateTime)) { e.Style.Format = "dd/MM/yy"; } base.OnPrepareViewStyleInfo(e); } But now, I can''t edit the date anymore. It seems that, when I key in a date like 07/12/04, the date is rejected by the DataTable. And when I key in a date like 07/12/2004, the date is rejected by the Grid. How can I do ?

4 Replies

AD Administrator Syncfusion Team December 7, 2004 01:03 PM UTC

In a GridDataBoundGrid, instead of using ColStyles, you need to use the GridBoundColumn.StyleInfo for the column instead. If you have explicitly added GridBoundColumns, then you get this styleinfo using grid.GridBoundColumns["MyColumnName"].StyleInfo. If you have not explicitly added GridBoundColumns, then you use grid.Binder.InternalColumns["MyColumnName"].StyleInfo to get at the style object. Once you have the style object, in addition to setting style.Format, you also will need to set the style.CellValueType to be DateTime if you want the formatting to ''take''. If you cannot set the CellvalueType for some reason (like your datasource does not contain a DateTime object), then you can handle the DrawCellDisplayText event, and there set e.Text to be the formatted string you want to see.

AD Administrator Syncfusion Team December 7, 2004 02:50 PM UTC

Thank you for answering. In my case, the DataTable change often, and the structure of the DataTable is always the same. So, as you advise, I have used the GridBoundColumns. Now, I dont have the date problem anymore, but I got another small problem. When the grid is bind to an empty DataTable (Table with columns but without row), the columns are displayed without the header text.

AD Administrator Syncfusion Team December 7, 2004 03:49 PM UTC

What version are you using? In, if I drop a GridDataboundGrid on a form, and then out this code in Form.load, I do not see a missing header. If you can post a sample showing the problem, maybe we can spot it.
private void Form1_Load(object sender, System.EventArgs e)
	DataTable dt = new DataTable("MyTable");
	int nCols = 4;
	for(int i = 0; i < nCols; i++)
		dt.Columns.Add(new DataColumn(string.Format("Col{0}", i)));

	this.gridDataBoundGrid1.GridBoundColumns.Add(new GridBoundColumn());
	this.gridDataBoundGrid1.GridBoundColumns[0].MappingName = "Col1";
	this.gridDataBoundGrid1.GridBoundColumns[0].HeaderText ="Some text";
	this.gridDataBoundGrid1.DataSource = dt;	 

RB Rodolphe Billottet December 8, 2004 12:56 PM UTC

Hello, I''m sorry, there was an error in my program. When I create an instance of GridBoundColumn, a lot of attributs of the instance are not defined. new GridBoundColumn(this.Binder.InternalColumns[colIndex].PropertyDescriptor); So, I prefer to modify the existing StyleInfo Binder.InternalColumns[].StyleInfo Now, all is OK! Thanks.

Live Chat Icon For mobile
Up arrow icon