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

object type cannot be converted to target type

Thread ID:

Created:

Updated:

Platform:

Replies:

55183 Jan 24,2007 07:19 PM UTC Jan 25,2007 10:04 PM UTC WinForms 3
loading
Tags: GridControl
Vivek
Asked On January 24, 2007 07:19 PM UTC

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



Administrator [Syncfusion]
Replied On 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

Administrator [Syncfusion]
Replied On 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

Administrator [Syncfusion]
Replied On 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

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

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

;