Posted 12 September 2018, 2:55 pm EST
How can I change a columns datatype to a date format at run time in a C1FlexGrid? I tried using typeof(datetime), which doesn’t work.
Forums Home / ComponentOne / WinForms Edition
Posted by: davidmorr on 12 September 2018, 2:55 pm EST
Posted 12 September 2018, 2:55 pm EST
How can I change a columns datatype to a date format at run time in a C1FlexGrid? I tried using typeof(datetime), which doesn’t work.
Posted 12 September 2018, 2:56 pm EST
C1FlexGrid1.Cols(8).DataType = typeof(DateTime) is what I tried.
Posted 13 September 2018, 1:05 am EST
Hi,
Changing DataType in unbound grid works correctly. So I’m assuming that you want to change DataType in bound mode. You can’t directly change a column’s DataType if it comes from a DataSource.
You can create another column in the DataSource or you can add an unbound column to the grid (with changed DataType). If you want to keep the values in unbound and corresponding bound column in sync, you can use GetUnboundValue and SetUnboundValue events like this (“Date” is the bound column with double data type):
private void _flex_SetUnboundValue(object sender, UnboundValueEventArgs e)
{
var row = _flex.Rows[e.Row].DataSource as DataRowView;
row["Date"] = Convert.ToDateTime(e.Value).ToOADate();
}
private void _flex_GetUnboundValue(object sender, UnboundValueEventArgs e)
{
var row = _flex.Rows[e.Row].DataSource as DataRowView;
e.Value = DateTime.FromOADate((double)row["Date"]);
}
You can refer to the attached sample for a complete example.
Regards,
Jitender
Posted 13 September 2018, 10:37 am EST - Updated 4 October 2022, 2:13 am EST
Thank you for your reply. Grid is not bound to a data source (e.g., it is unbound). When I use this statement C1FlexGrid1.Cols(8).DataType = typeof(DateTime), the error I receive is " ‘Date’ is a structure type and cannot be used as an expression." The structure referenced is System.DateTime. I’m using VB.NET 2015.
Posted 13 September 2018, 11:51 pm EST
Hi,
In VB, you need to use GetType(DateTime) instead of TypeOf(DateTime) to get the type of the argument.
Regards,
Jitender