datagridview apply cellstyle to cells

Posted by SchlaWiener on Stack Overflow See other posts from Stack Overflow or by SchlaWiener
Published on 2010-02-03T17:03:49Z Indexed on 2010/03/24 1:43 UTC
Read the original article Hit count: 608

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about datagridview