Samples for changing column StringFormat using CODE

Sep 4, 2008 at 11:48 AM

I am searching for a way to change the StringFormat of a datagrid column by CODE
The columns are autocreated and it would be nice if this does not have to change

Sep 4, 2008 at 12:39 PM
For the auto-generated columns scenario you can listen to the event, AutoGeneratingColumn, which gives you a chance to update or change the column that is generated.  This includes being able to change the column.HeaderStringFormat. 
Sep 4, 2008 at 12:51 PM

thanks for your answer, I found your article for comboboxcolumn and managed this using 


If Type.GetTypeCode(e.PropertyType) = System.TypeCode.DateTime Then



Dim oCol As New DataGridTextColumn



Dim oBinding As New Binding(e.PropertyName)


oBinding.StringFormat =

"dd.MM.yyyy hh:mm:ss"


oCol.DataFieldBinding = oBinding

oCol.Header = e.PropertyName

e.Column = oCol


End If


But this arises a further question: before using stringformat, I had the "wrong" format, but my "empty" dates (NULL values) have been empty in the grid, too

Now they have the value "01.01.0001 12:00:00"

Is there an easy way out there ?

Sep 4, 2008 at 8:03 PM
You could try to make the DateTime values nullable.  Otherwise they will need to have a default value. 
Sep 4, 2008 at 8:19 PM

thanks, this works

As I am a definite beginner, all this little peaces are very helpful.

Thank you for your support