object type cannot be converted to target type

Hi,

I have a Datetime Colum in GDBG. This is not a required column in my grid. If I initially enter a value in this column then late if i want to emptye this column/field then I am not able to delete the content of this field. I get the following message

“object type cannot be converted to target type”

I am having this problem with all the date time columns. Please help.

Thanks,
VS



3 Replies

AD Administrator Syncfusion Team January 24, 2007 08:50 PM UTC

Hi VS,

You can handle the CurrentCellValidating event and set the Render.ContolValue to DBNull.Value for empty text in a DateTime cell. Here is a code snippet.

private void gridDataBoundGrid1_CurrentCellValidating(object sender, CancelEventArgs e)
{
GridDataBoundGrid grid = sender as GridDataBoundGrid;
GridCurrentCell cc = grid.CurrentCell;
if( grid.Binder.InternalColumns[cc.ColIndex -1].MappingName == "DateColumn")
{
if( cc.Renderer.ControlText == String.Empty )
{
cc.Renderer.ControlValue = DBNull.Value;
grid.Model[cc.RowIndex,cc.ColIndex].CellValue = DBNull.Value;
}
}
}

Best Regards,
Haneef


AD Administrator Syncfusion Team January 24, 2007 10:56 PM UTC

Thanks Haneef,

It didn't work for me. Let me give you some more information.

My GDBG is bound to business collection. Whenever we change something on a cell we are immediacy updating our underlying business object, basically we are committing the changes on cell basis. My Grid Date time column is bound to a DateTime property on my business object.

I hope this could be helpful information for you.

Thanks,
VS



>Hi VS,

You can handle the CurrentCellValidating event and set the Render.ContolValue to DBNull.Value for empty text in a DateTime cell. Here is a code snippet.

private void gridDataBoundGrid1_CurrentCellValidating(object sender, CancelEventArgs e)
{
GridDataBoundGrid grid = sender as GridDataBoundGrid;
GridCurrentCell cc = grid.CurrentCell;
if( grid.Binder.InternalColumns[cc.ColIndex -1].MappingName == "DateColumn")
{
if( cc.Renderer.ControlText == String.Empty )
{
cc.Renderer.ControlValue = DBNull.Value;
grid.Model[cc.RowIndex,cc.ColIndex].CellValue = DBNull.Value;
}
}
}

Best Regards,
Haneef


AD Administrator Syncfusion Team January 25, 2007 10:04 PM UTC

Hi VS,

The reason for throwing the "object type cannot be converted to target type" exception is you are not able to store the String.Empty value or DBNull.Value for datetime value cell in a custom collection. But one way you can do this by replacing the string.Empty value in date column cell to DateTime.Min value in CurrentCellValidating event. and also you can handle the DrawCellDisplay event to show the empty text in a grid. Here is a code snippet

private void gridDataBoundGrid1_CurrentCellValidating(object sender, CancelEventArgs e)
{
GridDataBoundGrid grid = sender as GridDataBoundGrid;
GridCurrentCell cc = grid.CurrentCell;
if( grid.Binder.InternalColumns[cc.ColIndex -1].MappingName == "Date")
{
if( cc.Renderer.ControlText == String.Empty )
{
cc.Renderer.ControlValue = DateTime.MinValue;
grid.Model[cc.RowIndex,cc.ColIndex].CellValue = DateTime.MinValue;
EmptyDateCell[GetHashCode(cc.RowIndex,cc.ColIndex)] = true;
grid.Binder.EndEdit();
}
else
{
EmptyDateCell[GetHashCode(cc.RowIndex,cc.ColIndex)] = false;
}
}
}

Please refer to the attached sample for more details.
DateTimeStringEmptyValue.zip

Best Regards,
Haneef

Loader.
Up arrow icon