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

[Solved] Avoiding InvalidCastException in a custom edit form?

1 Answer 126 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 22 Jun 2009, 06:47 PM
I've put together a custom popup edit form using a template, and I'm having a problem with some checkboxes on it.  I have them declared in the template like so:

<asp:CheckBox ID="chkWriter" runat="server" Text="Writer" Checked='<%# Bind("Writer") %>'/> 

This works fine when I'm editing a record, but when I go to insert a new one I get an InvalidCastException here.

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 23 Jun 2009, 05:12 AM
Hello Chris,

The reason for this error is that your grid instance cannot bind a value for the newly inserted item through the Eval()/Bind() syntax you hard-coded. This can be fixed by presetting the default value of the control(s) when binding to a grid item on the RadGrid.InitInsertCommandName command 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("Writer") = false
 
        //Insert the item and rebind 
        e.Item.OwnerTableView.InsertItem(newValues); 
    } 

Thanks
Princy.
Tags
Grid
Asked by
Chris
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or