How do I add a DateTimePicker cell to a grid?
(Views :2921)

The DateTimePicker cell can be implemented by having a custom cell control which, is derived from the GridStaticCellModel and the GridStaticCellRenderer that uses the windows forms' DateTimePicker.

C#
//The cell model class
DateTimeCellModel : GridStaticCellMode
{
public DateTimeCellModel (GridModel grid): base(grid){ }
public override GridCellRendererBase CreateRenderer(GridControlBase control)
{
return new DateTimeCellRenderer(control, this);
}
}
//The cell renderer class
public class DateTimeCellRenderer : GridStaticCellRenderer
{
private MyDateTimePicker dateTimePicker;//MyDateTimePicker is a custom class derived from 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 & hide to make sure it is initilized properly for the first use...
dateTimePicker.Show();
dateTimePicker.Hide();
}
//Usual renderer overrides
//Handle drawing the cell
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);
}
}
//Set the value into the cell control & initialize it
protected override void OnInitialize(int rowIndex, int colIndex)
{
// Immediately 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();
}
//Save the changes from the cell control to the grid cell
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;
}
//Please refer to the attached sample for details regarding hiding DateTimePicker on cell deactivation, placing focus where user clicked in the control, custom implementation of DateTimePicker etc.
//Adding the custom cell model to the GridControl and using it as cell type
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);
}
VB
'The cell model class
Public Class DateTimeCellModel
Inherits GridStaticCellMode
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
'The cell renderer class
Public Class DateTimeCellRenderer
Inherits GridStaticCellRenderer
Private dateTimePicker As MyDateTimePicker 'MyDateTimePicker is a custom class derived from DateTimePicker
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 & hide to make sure it is initilized properly for the first use...
dateTimePicker.Show()
dateTimePicker.Hide()
End Sub
'Usual renderer overrides
'Handle drawing the cell
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
'Set the value into the cell control & initialize it
Protected Overrides Sub OnInitialize(ByVal rowIndex As Integer, ByVal colIndex As Integer)
' Immediately 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
'Save the changes from the cell control to the grid cell
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
'Please refer to the attached sample for details regarding hiding DateTimePicker on cell deactivation, placing focus where user clicked in the control, custom implementation of DateTimePicker etc.
//Adding the custom cell model to the GridControl and using it as cell type
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Me.gridControl1.CellModels.Add("DateTimePicker", New DateTimeCellModel(Me.gridControl1.Model))
Me.gridControl1(2,1).CellType = "DateTimePicker"
Me.gridControl1(2,1).CellValueType = GetType(DateTime)
End Sub

Here is a sample.

http://websamples.syncfusion.com/samples/KB/Grid.Windows/Grid_DateTimePickerCell/main.htm
::adCenter::