)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to customize the calendar cell type in WinForms GridControl?

Platform: WinForms |
Control: GridControl |
Published Date: May 10, 2016 |
Last Revised Date: December 16, 2019

Customize the calendar cell type

Grid 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"
 

 

Customize the calendar cell type in grid

 

Samples:

C#: CustomizeCalendar_CS

VB: CustomizeCalendar_VB

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon