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

Binding Boolean to CheckBox in RadGrid

3 Answers 639 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sanjay Gupta
Top achievements
Rank 1
Sanjay Gupta asked on 13 Feb 2010, 01:32 PM
Hi,
         I am binding Entity DataSource to RadGrid. Now the problem is when adding a new record, it throws "Specified cast is not available" Exception. it works fine for edit operation. I tried checking for null in getter of the property but the problem remains there. I am not able to figure out the problem.

                        <telerik:RadGrid ID="radProjectGrid" runat="server" AutoGenerateColumns="false" DataSourceID="entProjectDataSource" 
                            AllowAutomaticDeletes="true" AllowAutomaticInserts="true" AllowAutomaticUpdates="true" 
                            Skin="Default" OnItemCommand="radProjectGrid_ItemCommand"  
                            onitemcreated="radProjectGrid_ItemCreated"
                            <MasterTableView CommandItemDisplay="Top" AutoGenerateColumns="False" DataKeyNames="Project_ID" 
                                DataSourceID="entProjectDataSource"
                                <RowIndicatorColumn> 
                                    <HeaderStyle Width="20px" /> 
                                </RowIndicatorColumn> 
                                <ExpandCollapseColumn> 
                                    <HeaderStyle Width="20px" /> 
                                </ExpandCollapseColumn> 
                                <Columns> 
                                    <telerik:GridEditCommandColumn> 
                                    </telerik:GridEditCommandColumn> 
                                    <telerik:GridBoundColumn UniqueName="Project_Title" HeaderText="Project Title" DataField="Project_Title" 
                                        SortExpression="Project_Title"
                                        <HeaderStyle Width="150px" HorizontalAlign="Left" VerticalAlign="Middle" Wrap="true" /> 
                                        <ItemStyle Width="150px" HorizontalAlign="Left" VerticalAlign="Middle" Wrap="true" /> 
                                    </telerik:GridBoundColumn> 
                                    <telerik:GridBoundColumn DataField="ProjectLeader_Name" HeaderText="ProjectLeader Name" 
                                        SortExpression="ProjectLeader_Name" UniqueName="ProjectLeader_Name"
                                        <HeaderStyle Width="120px" HorizontalAlign="Left" VerticalAlign="Middle" Wrap="true" /> 
                                        <ItemStyle Width="120px" HorizontalAlign="Left" VerticalAlign="Middle" Wrap="true" /> 
                                    </telerik:GridBoundColumn> 
                                    <telerik:GridBoundColumn UniqueName="Location" HeaderText="Location" DataField="Location" 
                                        SortExpression="Location"
                                        <HeaderStyle Width="80px" HorizontalAlign="Left" VerticalAlign="Middle" Wrap="true" /> 
                                        <ItemStyle Width="80px" HorizontalAlign="Left" VerticalAlign="Middle" Wrap="true" /> 
                                    </telerik:GridBoundColumn> 
                                    <telerik:GridBoundColumn UniqueName="Start_Date" HeaderText="Start Date" DataField="Start_Date" 
                                        DataType="System.DateTime" SortExpression="Start_Date" DataFormatString="{0:MM/dd/yyyy}"
                                        <HeaderStyle Width="80px" HorizontalAlign="Left" VerticalAlign="Middle" Wrap="true" /> 
                                        <ItemStyle Width="80px" HorizontalAlign="Left" VerticalAlign="Middle" Wrap="true" /> 
                                    </telerik:GridBoundColumn> 
                                    <telerik:GridBoundColumn DataField="End_Date" DataType="System.DateTime" HeaderText="End Date" 
                                        SortExpression="End_Date" UniqueName="End_Date" DataFormatString="{0:MM/dd/yyyy}"
                                        <HeaderStyle Width="80px" HorizontalAlign="Left" VerticalAlign="Middle" Wrap="true" /> 
                                        <ItemStyle Width="80px" HorizontalAlign="Left" VerticalAlign="Middle" Wrap="true" /> 
                                    </telerik:GridBoundColumn> 
                                    <telerik:GridCheckBoxColumn DataField="IsVisible" DataType="System.Boolean" HeaderText="ShowOnWebsite" 
                                        SortExpression="IsVisible" UniqueName="IsVisible"
                                        <HeaderStyle Width="60px" HorizontalAlign="Left" VerticalAlign="Middle" Wrap="true" /> 
                                        <ItemStyle Width="50px" HorizontalAlign="Left" VerticalAlign="Middle" Wrap="true" /> 
                                    </telerik:GridCheckBoxColumn> 
                                    <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="column"
                                    </telerik:GridButtonColumn> 
                                </Columns> 
                                <EditFormSettings EditFormType="Template"
                                    <EditColumn UniqueName="EditCommandColumn1"
                                    </EditColumn> 
                                    <FormTemplate> 
                                        <div style="float: left"
                                            <table> 
                                                <tr> 
                                                    <td class="tdLeftAdmin"
                                                        Project Title: 
                                                    </td> 
                                                    <td class="tdRightAdmin"
                                                        <asp:TextBox ID="txtProjectTitle" runat="server" Text='<%# Bind("Project_Title") %>'></asp:TextBox> 
                                                    </td> 
                                                </tr> 
                                                <tr> 
                                                    <td class="tdLeftAdmin"
                                                        Location: 
                                                    </td> 
                                                    <td class="tdRightAdmin"
                                                        <asp:TextBox ID="txtLocation" runat="server" Text='<%# Bind("Location") %>'></asp:TextBox> 
                                                    </td> 
                                                </tr> 
                                                <tr> 
                                                    <td class="tdLeftAdmin"
                                                        Project Leader Name: 
                                                    </td> 
                                                    <td class="tdRightAdmin"
                                                        <asp:TextBox ID="txtProjectLeader" runat="server" Text='<%# Bind("ProjectLeader_Name") %>'></asp:TextBox> 
                                                    </td> 
                                                </tr> 
                                                <tr> 
                                                    <td class="tdLeftAdmin"
                                                        Start Date: 
                                                    </td> 
                                                    <td class="tdRightAdmin"
                                                        <telerik:RadDatePicker ID="rdpStartDate" runat="server" AutoPostBack="false" MinDate="01/01/1000" 
                                                            MaxDate="01/01/3000" DbSelectedDate='<%# Bind("Start_Date", "{0:D}") %>' /> 
                                                    </td> 
                                                </tr> 
                                                <tr> 
                                                    <td class="tdLeftAdmin"
                                                        End Date: 
                                                    </td> 
                                                    <td class="tdRightAdmin"
                                                        <telerik:RadDatePicker ID="rdpEndDate" runat="server" AutoPostBack="false" MinDate="01/01/1000" 
                                                            MaxDate="01/01/3000" DbSelectedDate='<%# Bind("End_Date","{0:D}") %>' /> 
                                                    </td> 
                                                </tr> 
                                                <tr> 
                                                    <td class="tdLeftAdmin"
                                                        Website: 
                                                    </td> 
                                                    <td class="tdRightAdmin"
                                                        <asp:TextBox ID="txtWebsite" runat="server"></asp:TextBox> 
                                                    </td> 
                                                </tr> 
                                                <tr> 
                                                    <td class="tdLeftAdmin"
                                                        ShowOnWebsite 
                                                    </td> 
                                                    <td class="tdRightAdmin"
                                                        <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("IsVisible") %>'/> 
                                                    </td> 
                                                </tr> 
                                                <tr> 
                                                    <td class="tdLeftAdmin"
                                                        <asp:Button ID="btnUpdate" runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' 
                                                            Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>' /> 
                                                    </td> 
                                                    <td class="tdRightAdmin"
                                                        <asp:Button ID="btnCancel" runat="server" CausesValidation="False" CommandName="Cancel" 
                                                            Text="Cancel" /> 
                                                    </td> 
                                                </tr> 
                                            </table> 
                                        </div> 
                                        <div style="width: 450px; float: right;"
                                            <table> 
                                                <tr> 
                                                    <td class="tdLeftAdmin"
                                                        Goal: 
                                                    </td> 
                                                    <td class="tdRightAdmin"
                                                        <asp:TextBox ID="txtGoal" runat="server" Text='<%# Bind("Goal") %>' TextMode="MultiLine"></asp:TextBox> 
                                                    </td> 
                                                </tr> 
                                                <tr> 
                                                    <td class="tdLeftAdmin"
                                                        Objective: 
                                                    </td> 
                                                    <td class="tdRightAdmin"
                                                        <asp:TextBox ID="txtObjective" runat="server" Text='<%# Bind("Objective") %>' TextMode="MultiLine"></asp:TextBox> 
                                                    </td> 
                                                </tr> 
                                                <tr> 
                                                    <td class="tdLeftAdmin"
                                                        Project Image: 
                                                    </td> 
                                                    <td class="tdRightAdmin"
                                                        <telerik:RadUpload ID="radUploadProjectImage" runat="server" ControlObjectsVisibility="None" 
                                                            TargetFolder="~/Images"
                                                        </telerik:RadUpload> 
                                                    </td> 
                                                </tr> 
                                                <tr> 
                                                    <td class="tdLeftAdmin"
                                                        Leader Image: 
                                                    </td> 
                                                    <td class="tdRightAdmin"
                                                        <telerik:RadUpload ID="radUploadProjectLeaderImage" runat="server" ControlObjectsVisibility="None" 
                                                            TargetFolder="~/Images"
                                                        </telerik:RadUpload> 
                                                    </td> 
                                                </tr> 
                                            </table> 
                                        </div> 
                                    </FormTemplate> 
                                </EditFormSettings> 
                            </MasterTableView> 
                            <ClientSettings> 
                                <ClientEvents OnRowDblClick="RowDblClick" /> 
                            </ClientSettings> 
                        </telerik:RadGrid> 


Ragards,
Thank  you in advance,

3 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 15 Feb 2010, 01:06 PM
Hi Sanjay,

I have already responded to your support ticket, and would ask you to close the current support thread. For the sake of other people that might stumble across this issue I am pasting my reply below:

----------------------------------

The reason for the invalid cast exception is that you have two-way binding for some of the data source fields such as Objective and Goal. And that's all right. However, when opening the insert form the grid tries to bind the controls in the editform to the value that is coming from the data-source and in insert mode there is no such value. Thus the grid tries to bind the controls in the editform to null and an invalid cast exception is thrown when null is being converted to System.String or any other type. 

In order to solve this issue, you need to set default insert values for the columns where you have two-way data-binding and need their fields updates/inserted in the database. How to do so is explained in the following help-article (here) (the paragraph Setting predefined values for column editors).

However, I need to caution you against a breaking changed that is coming with the new release scheduled for the beginning of March. If you are using the DefaultInsertValue column property approach described in the above help article, please, note that now it is of type System.Object. However, it turned out that there is a problem with the property type converter in Medium Trust, an issue that the .NET framework does not allow to circumvent. So we decided to change the property's type to String and with the Q1 2010 release the property will be of type System.String while preserving the same functionality and idea behind it. With the new release, the help topic will be updated accordingly.

------------------------------------------------

Sincerely yours,
Tsvetoslav
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
M
Top achievements
Rank 1
answered on 13 Sep 2011, 10:49 PM
The help topic link is broken.
Is there an updated link available?
0
Pavlina
Telerik team
answered on 14 Sep 2011, 07:54 AM
Hi,

You can find the correct link here:
http://www.telerik.com/help/aspnet-ajax/grid-inserting-values-inplace-and-editforms.html

Greetings,
Pavlina
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Sanjay Gupta
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
M
Top achievements
Rank 1
Pavlina
Telerik team
Share this question
or