This is a migrated thread and some comments may be shown as answers.

[Solved] Specified cast is not valid on checkbox in ajax grid

3 Answers 305 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mattias
Top achievements
Rank 1
Mattias asked on 21 Nov 2009, 05:09 PM
Hi,
I'm getting "Specified cast is not valid" when clicking "Add new".
When clicking on "Edit" on an existing row works fine.

<telerik:GridTemplateColumn UniqueName="CheckListChecked" HeaderText="<%$ Resources:resources,Label_Finished %>"
<ItemTemplate> 
      <asp:CheckBox ID="checkBoxCheckListChecked" Enabled="false" runat="server" Checked='<%# (Container is GridEditFormInsertItem) ? false : Eval("IsChecked") %>' /> 
</ItemTemplate> 
<EditItemTemplate> 
      <asp:CheckBox ID="checkBoxCheckListChecked" runat="server" Checked='<%# (Container is GridEditFormInsertItem) ? false : Eval("IsChecked") %>' /> 
</EditItemTemplate> 
</telerik:GridTemplateColumn> 

The checkbox in itemtemplate works but not the one in edititemtemplate when creating a new row. Why?

/Mattias

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 23 Nov 2009, 05:41 AM
Hello Mattias,

This error is found to occur when your grid instance cannot bind a value for the newly inserted item through the Eval()/Bind() syntax you have hard-coded. To avoid this error you can preset the value of the checkbox when binding to a grid item on the InitInsertCommand as shown below:
c#:
protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) 
     { 
    if ((e.CommandName == RadGrid.InitInsertCommandName))  
           { 
        e.Canceled = true
        //Prepare an IDictionary with the predefined values 
        System.Collections.Specialized.ListDictionary newValues = new System.Collections.Specialized.ListDictionary();       
 
        //set initial checked state for the checkbox on init insert 
        newValues("IsChecked") = false
 
        //Insert the item and rebind 
        e.Item.OwnerTableView.InsertItem(newValues); 
    } 

For reference, check out the following link:
Known reasons for error messages

Hope this helps...
Princy.
0
Mattias
Top achievements
Rank 1
answered on 23 Nov 2009, 08:01 AM
Thank you Princy.
It works! But it feels like a walk around, it would be great if the grid could check this itself in future release.
And it seems its only checkboxes that's not working (as far as I tested), cause I have this in the code as well and it doesn't complain about it.
<telerik:GridTemplateColumn UniqueName="CheckListName" HeaderText="<%$ Resources:resources,Label_Title %>"
<ItemTemplate> 
      <asp:Label ID="labelCheckListName" runat="server" Text='<%# Eval("Name") %>'></asp:Label> 
</ItemTemplate> 
<EditItemTemplate> 
      <asp:TextBox ID="textBoxCheckListName" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox> 
    <asp:RequiredFieldValidator ID="requiredFieldValidatorHeading" runat="server"  
ErrorMessage="obligatorisk" ControlToValidate="textBoxCheckListName" ValidationGroup="validationGroupCheckList"></asp:RequiredFieldValidator> 
</EditItemTemplate> 
</telerik:GridTemplateColumn> 

Regards,
Mattias
0
Nikolay Rusev
Telerik team
answered on 24 Nov 2009, 04:59 PM
Hello Mattias,

This is more extensibility point of RadGrid than a workaround.
However we are working on new feature of RadGrid that will enable you to set default values for insert form item declaratively per column.

Best wishes,
Nikolay
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Mattias
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Mattias
Top achievements
Rank 1
Nikolay Rusev
Telerik team
Share this question
or