'Specified cast is not valid' with custom editor

1 posts, 0 answers
  1. Dmitry
    Dmitry avatar
    1 posts
    Member since:
    Jun 2010

    Posted 06 Jun 2012 Link to this post

    Hi,
    I've tried to create a custom editor for TimeSpan data field and it returns an error 'Specified cast is not valid' when I try to set value.

    public class RadFilterTimeSpanEditor : RadFilterDataFieldEditor
        {
            RadMaskedTextBox _timePicker;
            public override void InitializeEditor(System.Web.UI.Control container)
            {
                _timePicker = new RadMaskedTextBox();
                _timePicker.Mask = "<0..5><0..9>:<0..5><0..9>";
                container.Controls.Add(_timePicker);
            }
            public override Type DataType
            {
                get
                {
                    return typeof(TimeSpan);
                }
                set  {}
            }
     
            public override System.Collections.ArrayList ExtractValues()
            {
                ArrayList list = new ArrayList(1);
                if (!string.IsNullOrWhiteSpace(_timePicker.Text))
                {             
                    TimeSpan ts = new TimeSpan(00, int.Parse(_timePicker.Text.Substring(0, 2)), int.Parse(_timePicker.Text.Substring(2, 2)));
                    list.Add(ts);
                }
           
                return list;
            }       
     
            public override void SetEditorValues(System.Collections.ArrayList values)
            {
                if (values != null && values.Count > 0)
                {
                    if (values[0] == null && !(values[0] is TimeSpan))
                        return;
                    TimeSpan ts = (TimeSpan)values[0];
                    _timePicker.Text = string.Format("{0:00}{1:00}", ts.Minutes, ts.Seconds);               
                }
            }
            protected override void CopySettings(RadFilterDataFieldEditor baseEditor)
            {
                base.CopySettings(baseEditor);
                var editor = baseEditor as RadFilterTimeSpanEditor;
                 
            }

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RadFilterTimeSpanEditor editorTimeSpan = new RadFilterTimeSpanEditor();
                RadFilter1.FieldEditors.Add(editorTimeSpan);
                editorTimeSpan.FieldName = "ShipingDuration";
                editorTimeSpan.DisplayName = "Shipping Duration";          
            }
        }
        protected void RadFilter1_FieldEditorCreating(object sender, Telerik.Web.UI.RadFilterFieldEditorCreatingEventArgs e)
        {
            if (e.EditorType == "RadFilterTimeSpanEditor")
            {
                e.Editor = new RadFilterTimeSpanEditor();
            }
        }
    }

    <telerik:RadFilter ID="RadFilter1" runat="server" CssClass="RadFilter RadFilter_Default RadFilter RadFilter_Default "
                FilterContainerID="RadGrid1" OnFieldEditorCreating="RadFilter1_FieldEditorCreating">
            </telerik:RadFilter>
            <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                CellSpacing="0" DataSourceID="ObjectDataSource1" GridLines="None">
                <ClientSettings>
                    <Selecting CellSelectionMode="None"></Selecting>
                </ClientSettings>
                <MasterTableView DataSourceID="ObjectDataSource1">
                    <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
                    <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
                        <HeaderStyle Width="20px"></HeaderStyle>
                    </RowIndicatorColumn>
                    <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
                        <HeaderStyle Width="20px"></HeaderStyle>
                    </ExpandCollapseColumn>
                    <Columns>
                        <telerik:GridBoundColumn DataField="OrderID" DataType="System.Int32" FilterControlAltText="Filter OrderID column"
                            HeaderText="OrderID" SortExpression="OrderID" UniqueName="OrderID">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="CustomerID" FilterControlAltText="Filter CustomerID column"
                            HeaderText="CustomerID" SortExpression="CustomerID" UniqueName="CustomerID">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="EmployeeID" DataType="System.Int32" FilterControlAltText="Filter EmployeeID column"
                            HeaderText="EmployeeID" SortExpression="EmployeeID" UniqueName="EmployeeID">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="OrderDate" DataType="System.DateTime" FilterControlAltText="Filter OrderDate column"
                            HeaderText="OrderDate" SortExpression="OrderDate" UniqueName="OrderDate">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="RequiredDate" DataType="System.DateTime" FilterControlAltText="Filter RequiredDate column"
                            HeaderText="RequiredDate" SortExpression="RequiredDate" UniqueName="RequiredDate">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="ShippedDate" DataType="System.DateTime" FilterControlAltText="Filter ShippedDate column"
                            HeaderText="ShippedDate" SortExpression="ShippedDate" UniqueName="ShippedDate">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="ShipVia" DataType="System.Int32" FilterControlAltText="Filter ShipVia column"
                            HeaderText="ShipVia" SortExpression="ShipVia" UniqueName="ShipVia">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Freight" DataType="System.Decimal" FilterControlAltText="Filter Freight column"
                            HeaderText="Freight" SortExpression="Freight" UniqueName="Freight">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="ShipName" FilterControlAltText="Filter ShipName column"
                            HeaderText="ShipName" SortExpression="ShipName" UniqueName="ShipName">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="ShipAddress" FilterControlAltText="Filter ShipAddress column"
                            HeaderText="ShipAddress" SortExpression="ShipAddress" UniqueName="ShipAddress">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="ShipCity" FilterControlAltText="Filter ShipCity column"
                            HeaderText="ShipCity" SortExpression="ShipCity" UniqueName="ShipCity">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="ShipRegion" FilterControlAltText="Filter ShipRegion column"
                            HeaderText="ShipRegion" SortExpression="ShipRegion" UniqueName="ShipRegion">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="ShipPostalCode" FilterControlAltText="Filter ShipPostalCode column"
                            HeaderText="ShipPostalCode" SortExpression="ShipPostalCode" UniqueName="ShipPostalCode">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="ShipCountry" FilterControlAltText="Filter ShipCountry column"
                            HeaderText="ShipCountry" SortExpression="ShipCountry" UniqueName="ShipCountry">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="ShipingDuration" DataType="System.TimeSpan" FilterControlAltText="Filter ShipingDuration column"
                            HeaderText="ShipingDuration" UniqueName="ShipingDuration" SortExpression="ShipingDuration">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="EntityState" DataType="System.Int32" FilterControlAltText="Filter EntityState column"
                            HeaderText="EntityState" ReadOnly="True" SortExpression="EntityState" UniqueName="EntityState">
                        </telerik:GridBoundColumn>
                    </Columns>
                    <EditFormSettings>
                        <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                        </EditColumn>
                    </EditFormSettings>
                </MasterTableView>
                <FilterMenu EnableImageSprites="False">
                </FilterMenu>
            </telerik:RadGrid>
        </div>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetOrders"
            TypeName="NORTHWINDModel.OrdersAdapter"></asp:ObjectDataSource>

    public partial class Orders
        {
            public TimeSpan? ShipingDuration
            {
                get
                {
                    return this.ShippedDate - this.OrderDate;
                }
                set { }
            }
        }
Back to Top