Category / Section
How to customize the calendar cell type in WinForms GridControl?
2 mins read
Customize the calendar cell type
GridControl does not have DateTimePicker CellType. To set the DateTimePicker cell type, you can create a custom cell model/renderer that implements the DateTimePicker CellType and the style of the DateTimePicker can be customized using the renderer.
Creating CellModel
C#
public class DateTimeCellModel : GridStaticCellModel { public DateTimeCellModel (GridModel grid) : base(grid) { } public override GridCellRendererBase CreateRenderer(GridControlBase control) { return new DateTimeCellRenderer(control, this); } }
VB
Public Class DateTimeCellModel Inherits GridStaticCellModel Public Sub New(ByVal grid As GridModel) MyBase.New(grid) End Sub Public Overrides Function CreateRenderer(ByVal control As GridControlBase) As GridCellRendererBase Return New DateTimeCellRenderer(control, Me) End Function End Class
Creating CellRenderer
In the custom cell renderer constructor, the customization of the cell is achieved.
C#
public class DateTimeCellRenderer : GridStaticCellRenderer { private MyDateTimePicker dateTimePicker; public DateTimeCellRenderer(GridControlBase grid, GridCellModelBase cellModel) : base(grid, cellModel) { dateTimePicker = new MyDateTimePicker(); dateTimePicker.Format = System.Windows.Forms.DateTimePickerFormat.Custom; dateTimePicker.ShowUpDown = false; dateTimePicker.ShowCheckBox = false; dateTimePicker.ShowDropButton = true; dateTimePicker.Border3DStyle = Border3DStyle.Flat; grid.Controls.Add(dateTimePicker); //show & hide to make sure it is initialized properly for the first use... dateTimePicker.Show(); dateTimePicker.Hide(); Grid.HScrollBar.ValueChanged += new EventHandler(HScrollBar_ValueChanged); Grid.VScrollBar.ValueChanged += new EventHandler(VScrollBar_ValueChanged); } void HScrollBar_ValueChanged(object sender, EventArgs e) { int row, col; Grid.PointToRowCol(dateTimePicker.Location, out row, out col); if (!Grid.ViewLayout.VisibleCellsRange.IntersectsWith(GridRangeInfo.Cell(row, col)) || (row <= this.Grid.Model.Rows.HeaderCount) || !(row.Equals(RowIndex) && col.Equals(ColIndex))) { this.dateTimePicker.Hide(); this.dateTimePicker.ShowDropButton = false; } else { this.dateTimePicker.Show(); this.dateTimePicker.ShowDropButton = true; } } void VScrollBar_ValueChanged(object sender, EventArgs e) { int row, col; Grid.PointToRowCol(dateTimePicker.Location, out row, out col); if (!Grid.ViewLayout.VisibleCellsRange.IntersectsWith(GridRangeInfo.Cell(row, col)) || (row <= this.Grid.Model.Rows.HeaderCount) || !(row.Equals(RowIndex) && col.Equals(ColIndex))) { this.dateTimePicker.Hide(); this.dateTimePicker.ShowDropButton = false; } else { this.dateTimePicker.Show(); this.dateTimePicker.ShowDropButton = true; } } }
VB
Public Class DateTimeCellRenderer Inherits GridStaticCellRenderer Private dateTimePicker As MyDateTimePicker Public Sub New(ByVal grid As GridControlBase, ByVal cellModel As GridCellModelBase) MyBase.New(grid, cellModel) dateTimePicker = New MyDateTimePicker() dateTimePicker.Format = System.Windows.Forms.DateTimePickerFormat.Custom dateTimePicker.ShowUpDown = False dateTimePicker.ShowCheckBox = False dateTimePicker.ShowDropButton = True dateTimePicker.Border3DStyle = Border3DStyle.Flat grid.Controls.Add(dateTimePicker) 'show & hide to make sure it is initialized properly for the first use... dateTimePicker.Show() dateTimePicker.Hide() AddHandler Grid.HScrollBar.ValueChanged, AddressOf HScrollBar_ValueChanged AddHandler Grid.VScrollBar.ValueChanged, AddressOf VScrollBar_ValueChanged End Sub Private Sub HScrollBar_ValueChanged(ByVal sender As Object, ByVal e As EventArgs) Dim row, col As Integer Grid.PointToRowCol(dateTimePicker.Location, row, col) If (Not Grid.ViewLayout.VisibleCellsRange.IntersectsWith(GridRangeInfo.Cell(row, col))) OrElse (row <= Me.Grid.Model.Rows.HeaderCount) OrElse Not(row.Equals(RowIndex) AndAlso col.Equals(ColIndex)) Then Me.dateTimePicker.Hide() Me.dateTimePicker.ShowDropButton = False Else Me.dateTimePicker.Show() Me.dateTimePicker.ShowDropButton = True End If End Sub Private Sub VScrollBar_ValueChanged(ByVal sender As Object, ByVal e As EventArgs) Dim row, col As Integer Grid.PointToRowCol(dateTimePicker.Location, row, col) If (Not Grid.ViewLayout.VisibleCellsRange.IntersectsWith(GridRangeInfo.Cell(row, col))) OrElse (row <= Me.Grid.Model.Rows.HeaderCount) OrElse Not(row.Equals(RowIndex) AndAlso col.Equals(ColIndex)) Then Me.dateTimePicker.Hide() Me.dateTimePicker.ShowDropButton = False Else Me.dateTimePicker.Show() Me.dateTimePicker.ShowDropButton = True End If End Sub End Class
Adding CellModel and Assigning CellType
C#
//Adding CellModel this.gridControl1.CellModels.Add("DateTimePicker", new DateTimeCellModel(this.gridControl1.Model)); //Assigning CellType this.gridControl1[rowIndex, colIndex].CellType = "DateTimePicker";
VB
'Adding CellModel Me.gridControl1.CellModels.Add("DateTimePicker", New DateTimeCellModel(Me.gridControl1.Model)) 'Assigning CellType Me.gridControl1(rowIndex, colIndex).CellType = "DateTimePicker"