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. Image for the cookie policy date

Custom DateTimeCell cell type

I'm trying to implement your DateTimeCellModel/Renderer classes (..\Samples\2.0\CustomCellTypes\DateTimePickerCells\CS\) and am mostly having some success except for a couple of issues that I could use some help on.
#1) When I'm focused on one DateTimeCell and move to another, the second cell is rendered with the value of the cell I was just on. Is there a known way to prevent this?
#2) Once I pick a value, I can't seem to lose focus/exit the cell unless I choose a different (non-DateTimeCell) cell in the same grid. That is, I can't click on to a different control on my form to lose focus.
#3) I' had some trouble with setting null values using the 'None' button in the drop down but I think I've resolved them with this code:

In OnInitialize:
if ( !Convert.IsDBNull (style.CellValue))
dateTimePicker.Value = (DateTime)style.CellValue;

In OnSave:
if (dateTimePicker.IsNullDate)
style.CellValue = DBNull.Value;
else
style.CellValue = dateTimePicker.Value;



3 Replies

AD Administrator Syncfusion Team November 25, 2007 09:06 AM UTC

>>In OnInitialize:
if ( !Convert.IsDBNull (style.CellValue))
dateTimePicker.Value = (DateTime)style.CellValue;


What value do you want to see in the dateTimePicker when the cell contains DBNull? If you set no value, then the dateTimePicker will retain the value that it last had. This might explain why you are seeing the last value when you click on an empty cell.

Try this code. In the DateTimeCellRenderer constructor, add:

dateTimePicker.NullString = "";


Then use this OnInitialize:

protected override void OnInitialize(int rowIndex, int colIndex)
{
// Immeditaly switch into editing mode when cell is initialized.
GridStyleInfo style = Grid.Model[rowIndex, colIndex];

if (style.Text.Length > 0 && !Convert.IsDBNull(style.CellValue))
dateTimePicker.Value = (DateTime)style.CellValue;
else
dateTimePicker.IsNullDate = true;

CurrentCell.BeginEdit();
base.OnInitialize(rowIndex, colIndex);
dateTimePicker.ValueChanged += new EventHandler(datePicker_ValueChanged);
dateTimePicker.ShowDropButton = style.ShowButtons != GridShowButtons.Hide;
dateTimePicker.Update();
}






MA Mark Atkinson November 26, 2007 05:02 PM UTC

I was using the NullString and checking for IsDBNull but I wasn't implementing an else condition in my OnInitialize method to set the IsNullDate = true

Thanks for your help, this is working well.
By the way, I think this model/renderer idea for implementing custom cell types is great. It's come in handy for me for a few different cell types I've created.

>>>In OnInitialize:
if ( !Convert.IsDBNull (style.CellValue))
dateTimePicker.Value = (DateTime)style.CellValue;


What value do you want to see in the dateTimePicker when the cell contains DBNull? If you set no value, then the dateTimePicker will retain the value that it last had. This might explain why you are seeing the last value when you click on an empty cell.

Try this code. In the DateTimeCellRenderer constructor, add:

dateTimePicker.NullString = "";


Then use this OnInitialize:

protected override void OnInitialize(int rowIndex, int colIndex)
{
// Immeditaly switch into editing mode when cell is initialized.
GridStyleInfo style = Grid.Model[rowIndex, colIndex];

if (style.Text.Length > 0 && !Convert.IsDBNull(style.CellValue))
dateTimePicker.Value = (DateTime)style.CellValue;
else
dateTimePicker.IsNullDate = true;

CurrentCell.BeginEdit();
base.OnInitialize(rowIndex, colIndex);
dateTimePicker.ValueChanged += new EventHandler(datePicker_ValueChanged);
dateTimePicker.ShowDropButton = style.ShowButtons != GridShowButtons.Hide;
dateTimePicker.Update();
}








MA Mark Atkinson November 26, 2007 10:14 PM UTC

Another small thing I added to the OnInitialize method was this:

if (dateTimePicker.Focused)
CurrentCell.BeginEdit();

...calling BeginEdit without some check seems to force the focus in the cell when you've clicked on another control in the parent form.


Loader.
Live Chat Icon For mobile
Up arrow icon