views:

24

answers:

1

I used this example to create a DateTime column for a DataGridView in my winforms app.

http://msdn.microsoft.com/en-us/library/7tas5c80.aspx

I can see the new column in the Windows Forms Designer and add it to an existing DataGridView. However, I want to be able to change the display format when I change the "DefaultCellStyle" within the designer.

The designer generated code looks like this:

    DataGridViewCellStyle1.Format = "t"
    DataGridViewCellStyle1.NullValue = Nothing
    Me.colDate.DefaultCellStyle = DataGridViewCellStyle1
    Me.colDatum.Name = "colDate"
    Me.colDatum.Resizable = System.Windows.Forms.DataGridViewTriState.[False]

Which is fine. But since the code of the DataGridViewCalendarCell does this in the constructor:

    Public Sub New()
        Me.Style.Format = "d"
    End Sub

The format never changes to "t" (time format). I didn't find out how to apply the format from the owning column to I use this woraround atm:

    Public Overrides Function GetInheritedStyle _
            (ByVal inheritedCellStyle As _
                  System.Windows.Forms.DataGridViewCellStyle, _
            ByVal rowIndex As Integer, ByVal includeColors As Boolean) _
    As System.Windows.Forms.DataGridViewCellStyle

        If Me.OwningColumn IsNot Nothing Then
            Me.Style.Format = Me.OwningColumn.DefaultCellStyle.Format
        End If

        Return MyBase.GetInheritedStyle(_
            inheritedCellStyle, rowIndex, includeColors)

    End Function

However, since this is just an hack I want to know which is the "how it should" be done way to apply the default cellstyle from a DataGridViewColumn to its cells. Any suggestions?

A: 

I would remove the constructor. The problem is the constructor is implicity creating a style. Therefore, the format of the default style will not be used. This is actually a "resource hog" because each cell is going to have its own style, instead of all the cells sharing the same style.

Removing the constructor will solve your problem, but if you want to default to "short date" then maybe overriding GetFormattedValue is the better implementation. Such as the following example. I want to stress that it is an example only. You make have to work out the details.

    Protected Overrides Function GetFormattedValue( _
        ByVal value As Object, _
        ByVal rowIndex As Integer, _
        ByRef cellStyle As System.Windows.Forms.DataGridViewCellStyle, _
        ByVal valueTypeConverter As System.ComponentModel.TypeConverter, _
        ByVal formattedValueTypeConverter As System.ComponentModel.TypeConverter, _
        ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object

        If (cellStyle IsNot Nothing) OrElse (String.IsNullOrEmpty(cellStyle.Format)) Then
            'no format specified, so default to short date
            cellStyle.Format = "d"
        End If

        Return MyBase.GetFormattedValue(value, rowIndex, cellStyle, valueTypeConverter, formattedValueTypeConverter, context)

    End Function
AMissico