ver. 2010 R1 Winforms
I am trying to set the date format for a DateTime column that is being loaded from a SQL Server result set. Of course SQL is sending the full date and time (ie 1/1/2000 12:00.00AM) but I want to display only "1/1/2000".
I have set the column .Format string to "d" and a number of other format options and the grid still displays the raw text coming from SQL Server without formatting. None of the columns on the grid are editable.
Here is my code:
//Load datagrid.DataSource = ds;grid.DataMember = "Table";grid.DataBind();
//Set formatgrid.DisplayLayout.Bands[0].Columns["MyDate"].Format = "d";
I have tried placing the formatting line in the InitializeLayout event handler but it made no difference. Other column properties such as .Hidden work just fine in the context above. It is the .Format that is ignoring anything I put in it.
Thanks.
Eric
Thanks for the help, both of you guys. I tried setting the column style. I checked the DataType in the debugger and it was set to System.DateTime, so as last resort I started looking for places where I was overriding the format--knowing that there was little chance since this is a real simple implementation. But...while carefully looking through the designer generated code I found that I had set CellDisplayStyle to "PlainText" . Duh. So obviously formatting would be ignored. Once I changed the value to "FormattedText" my problem resolved.Thanks Mike for nudging me to look deeper at my code.
If the Format property is having no effect, then my first guess is that your column does not contain dates, but instead contains strings. Check the DataType property on the column.
If that's not the case, then my next guess is that something else in your code is overwriting or resetting the Format property after you are setting it.
Try setting the ColumnStyle of the column to date.
grid.DisplayLayout.Bands[0].Columns["MyDate"].Style = ColumnStyle.Date