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: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to add a DateTimePicker in Grid?

Platform: WinForms |
Control: GridControl |
Published Date: April 30, 2011 |
Last Revised Date: May 28, 2019

The DateTimePicker cell can be implemented by having a custom cell control that is derived from the GridStaticCellModel and the GridStaticCellRenderer that uses the Windows Forms DateTimePicker.

Creating CellModel: Use the following code example for creating CustomCellModel.

C#

public class DateTimeCellModel : GridStaticCellModel
{
public DateTimeCellModel (GridModel grid)
: base(grid)
{
}
public override GridCellRendererBase CreateRenderer(GridControlBase control)
{
return new DateTimeCellRenderer(control, this);
}
}

VB

'the cell model class
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: Use the following code example for custom cell renderer.

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 = true;
dateTimePicker.ShowCheckBox = false;
grid.Controls.Add(dateTimePicker);
//show and hide to make sure it is initialized properly for the first use...
dateTimePicker.Show();
dateTimePicker.Hide();
}
#region usual renderer overrides . . .
#endregion
#region Click stuff . . .
#endregion
//handle initial keystroke on inactive cell, pass it to the DateTimePicker
protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)  {
if(!dateTimePicker.Focused)
{
dateTimePicker.Focus();
SendKeys.Send(new string(e.KeyChar, 1));
}
base.OnKeyPress(e);
}
}

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 = True
dateTimePicker.ShowCheckBox = False
grid.Controls.Add(dateTimePicker)
'show and hide to make sure it is initialized properly for the first use...
dateTimePicker.Show()
dateTimePicker.Hide()
End Sub
#Region "usual renderer overrides". . .
#End Region
#Region "Click stuff". . .
#End Region
'handle initial keystroke on inactive cell, pass it to the DateTimePicker
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
If Not dateTimePicker.Focused Then
dateTimePicker.Focus()
SendKeys.Send(New String(e.KeyChar, 1))
End If
MyBase.OnKeyPress(e)
End Sub
End Class

Event Overrides

OnDraw: To handle drawing on cell.

C#

protected override void OnDraw(System.Drawing.Graphics g, System.Drawing.Rectangle clientRectangle, int rowIndex, int colIndex, Syncfusion.Windows.Forms.Grid.GridStyleInfo style)
{
if (Grid.CurrentCell.HasCurrentCellAt(rowIndex, colIndex) && CurrentCell.IsEditing)
{
dateTimePicker.Size = clientRectangle.Size;
dateTimePicker.CustomFormat = style.Format;
dateTimePicker.Font = style.GdipFont;
dateTimePicker.Location = clientRectangle.Location;
dateTimePicker.Show();
if (!dateTimePicker.ContainsFocus)
dateTimePicker.Focus();
}
else
{
style.TextMargins.Left = 3; //avoid the little jump...
base.OnDraw(g, clientRectangle, rowIndex, colIndex, style);
}
}

VB

Protected Overrides Sub OnDraw(ByVal g As System.Drawing.Graphics, ByVal clientRectangle As System.Drawing.Rectangle, ByVal rowIndex As Integer, ByVal colIndex As Integer, ByVal style As Syncfusion.Windows.Forms.Grid.GridStyleInfo)
If Grid.CurrentCell.HasCurrentCellAt(rowIndex, colIndex) AndAlso CurrentCell.IsEditing Then
dateTimePicker.Size = clientRectangle.Size
dateTimePicker.CustomFormat = style.Format
dateTimePicker.Font = style.GdipFont
dateTimePicker.Location = clientRectangle.Location
dateTimePicker.Show()
If Not dateTimePicker.ContainsFocus Then
dateTimePicker.Focus()
End If
Else
style.TextMargins.Left = 3 'avoid the little jump...
MyBase.OnDraw(g, clientRectangle, rowIndex, colIndex, style)
End If
End Sub

OnInitialize: To set value into the cell control and initialize it.

C#

protected override void OnInitialize(int rowIndex, int colIndex)
{
// Immeditaly switch into editing mode when cell is initialized.
GridStyleInfo style = Grid.Model[rowIndex, colIndex];
dateTimePicker.Value = (DateTime) style.CellValue;
CurrentCell.BeginEdit();
base.OnInitialize(rowIndex, colIndex);
dateTimePicker.ValueChanged += new EventHandler(datePicker_ValueChanged);
dateTimePicker.Update();
}

VB

Protected Overrides Sub OnInitialize(ByVal rowIndex As Integer, ByVal colIndex As Integer)
' Immeditaly switch into editing mode when cell is initialized.
Dim style As GridStyleInfo = Grid.Model(rowIndex, colIndex)
dateTimePicker.Value = CDate(style.CellValue)
CurrentCell.BeginEdit()
MyBase.OnInitialize(rowIndex, colIndex)
AddHandler dateTimePicker.ValueChanged, AddressOf datePicker_ValueChanged
dateTimePicker.Update()
End Sub

OnSaveChanges: It saves the changes from cell control to grid cell.

C#

protected override bool OnSaveChanges()
{
if (CurrentCell.IsModified)
{
Grid.Focus();
GridStyleInfo style = Grid.Model[this.RowIndex, this.ColIndex];
style.CellValue = this.dateTimePicker.Value;;
return true;
}
return false;
}

VB

Protected Overrides Function OnSaveChanges() As Boolean
If CurrentCell.IsModified Then
Grid.Focus()
Dim style As GridStyleInfo = Grid.Model(Me.RowIndex, Me.ColIndex)
style.CellValue = Me.dateTimePicker.Value
Return True
End If
Return False
End Function

OnDeactived: To hide the control.

C#

protected override void OnDeactived(int rowIndex, int colIndex)
{
if(dateTimePicker.Visible)
{
this.dateTimePicker.Hide();
Grid.Focus();
}
dateTimePicker.ValueChanged -= new EventHandler(datePicker_ValueChanged);
}

VB

Protected Overrides Sub OnDeactived(ByVal rowIndex As Integer, ByVal colIndex As Integer)
If dateTimePicker.Visible Then
Me.dateTimePicker.Hide()
Grid.Focus()
End If
RemoveHandler dateTimePicker.ValueChanged, AddressOf datePicker_ValueChanged
End Sub

DateTimePicker Customization

C#

public class MyDateTimePicker : System.Windows.Forms.DateTimePicker
{
public bool keyPressed = false;
//pass the enter key back to the grid....
//and trigger change event on other keystrokes...
protected override bool ProcessCmdKey(ref Message msg,Keys keyData)
{
if(keyData == Keys.Enter)
return false;
if(msg.Msg == 0x100 && keyData != Keys.Tab) //keydown...
this.OnValueChanged(EventArgs.Empty);
return base.ProcessCmdKey(ref msg, keyData);
}
}

VB

Public Class MyDateTimePicker
Inherits System.Windows.Forms.DateTimePicker
Public keyPressed As Boolean = False
'pass the enter key back to the grid....
'and trigger change event on other keystrokes...
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
If keyData = Keys.Enter Then
Return False
End If
If msg.Msg = &H100 AndAlso keyData <> Keys.Tab Then 'keydown...
Me.OnValueChanged(EventArgs.Empty)
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
End Class

Adding CellModel

The following code example is used for adding the custom cell model to the GridControl and using it as cell type.

C#

private void Form1_Load(object sender, System.EventArgs e)
{
this.gridControl1.CellModels.Add("DateTimePicker", new DateTimeCellModel(this.gridControl1.Model));
this.gridControl1[2,1].CellType = "DateTimePicker";
this.gridControl1[2,1].CellValueType = typeof(DateTime);
this.gridControl1[2,1].CellValue = DateTime.Now.AddDays(0);
//To display the time.
this.gridControl1[2,1].Format = "h:mm tt";
this.gridControl1.ColWidths[1] = 75;
this.gridControl1[3,1].CellType = "DateTimePicker";
this.gridControl1[3,1].CellValueType = typeof(DateTime);
this.gridControl1[3,1].CellValue = DateTime.Now.AddDays(0);
//To display date.
this.gridControl1[3,1].Format = "dd:MM:yyyy";
this.gridControl1.ColWidths[1] = 75;
}

VB

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.gridControl1.CellModels.Add("DateTimePicker", New DateTimeCellModel(Me.gridControl1.Model))
Me.gridControl1(2, 1).CellType = "DateTimePicker"
Me.gridControl1(2,1).CellValueType = GetType(DateTime)
Me.gridControl1(2,1).CellValue = DateTime.Now.AddDays(0)
'To display the time.
Me.gridControl1(2,1).Format = "h:mm tt"
Me.gridControl1.ColWidths(1) = 75
Me.gridControl1(3,1).CellType = "DateTimePicker"
Me.gridControl1(3,1).CellValueType = GetType(DateTime)
Me.gridControl1(3,1).CellValue = DateTime.Now.AddDays(0)
'To display date.
Me.gridControl1(3,1).Format = "dd:MM:yyyy"
Me.gridControl1.ColWidths(1) = 75
End Sub

The following screenshot illustrates the DataTimePicker cell in GridControl.

Showing DateTimePicker in GridControl

Figure 1: DataTimePicker cell in GridControl

Sample:

C#: DateTimePicker CS

VB: DateTimePicker 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