Articles in this section
Category / Section

How to add a DateTimePicker in Grid?

4 mins read

The DateTimePicker cell can be implemented in WinForms GridControl 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 WinForms GridControl

Figure 1: DataTimePicker cell in GridControl

Sample:

C#: DateTimePicker CS

VB: DateTimePicker VB

 

Conclusion

I hope you enjoyed learning about how to add a DateTimePicker in Grid.

You can refer to our WinForms GridControl‚Äôs feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms GridControl documentation to understand how to present and manipulate data.


For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms GridControl and other WinForms components.


If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment