I am exporting a RadGrid to Excel and I am receiving the following error:
Invalid cast from 'Char' to 'Double'.Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: Invalid cast from 'Char' to 'Double'. 3 Answers, 1 is accepted
0
Sean
Top achievements
Rank 1
answered on 09 Dec 2015, 06:33 PM
...no way to edit a post eh? Anyways, the following is the code for the Grid:
<telerik:RadGrid AllowPaging="true" PageSize="40" OnItemCommand="rg_ItemCommand" Height="600" ID="rg" MasterTableView-AllowFilteringByColumn="true" MasterTableView-AllowSorting="true" runat="server" AllowFilteringByColumn="true" AllowSorting="true"> <MasterTableView AutoGenerateColumns="false" HeaderStyle-Width="120" AlternatingItemStyle-Wrap="true"> <Columns> <telerik:GridBoundColumn ItemStyle-Wrap="false" DataField="BillName" HeaderText="Customer" UniqueName="BillName"> </telerik:GridBoundColumn> <telerik:GridBoundColumn ItemStyle-Wrap="false" DataField="Addr1" Display="True" HeaderText="Site" UniqueName="Addr1"> </telerik:GridBoundColumn> <telerik:GridBoundColumn ItemStyle-HorizontalAlign="Center" DataField="ContractID" Display="true" HeaderText="Contract ID" UniqueName="CallID"> </telerik:GridBoundColumn> <telerik:GridBoundColumn ItemStyle-HorizontalAlign="Center" DataField="SiteID" Display="true" HeaderText="Site ID" UniqueName="SiteID"> </telerik:GridBoundColumn> <telerik:GridBoundColumn ItemStyle-HorizontalAlign="Center" DataField="CustID" Display="false" HeaderText="CustID" UniqueName="CustID"> </telerik:GridBoundColumn> <telerik:GridBoundColumn ItemStyle-HorizontalAlign="Center" DataFormatString="{0:d}" DataField="EffectiveDate" HeaderText="Effective Date" UniqueName="EffectiveDate"> </telerik:GridBoundColumn> <telerik:GridBoundColumn ItemStyle-HorizontalAlign="Center" DataFormatString="{0:d}" Display="false" DataField="ExpireDate" HeaderText="Expire Date" MaxLength="50" UniqueName="ExpireDate"> </telerik:GridBoundColumn> <telerik:GridBoundColumn ItemStyle-HorizontalAlign="Right" DataFormatString="{0:C}" DataField="TotalAmt" HeaderText="Total Amount" MaxLength="50" UniqueName="TotalAmt"> </telerik:GridBoundColumn> <telerik:GridBoundColumn ItemStyle-Wrap="false" DataFormatString="{0:C}" ItemStyle-HorizontalAlign="Right" DataField="TotalBilled" HeaderText="Total Billed" MaxLength="50" UniqueName="TotalBilled"> </telerik:GridBoundColumn> <telerik:GridBoundColumn ItemStyle-HorizontalAlign="Center" DataField="Status" HeaderText="Contract Status" MaxLength="50" UniqueName="Status"> </telerik:GridBoundColumn> <telerik:GridBoundColumn ItemStyle-HorizontalAlign="Center" DataFormatString="{0:d}" DataField="Lupd_DateTime" HeaderText="Last Updated" MaxLength="50" UniqueName="Lupd_DateTime"> </telerik:GridBoundColumn> </Columns> </MasterTableView> <ClientSettings EnableRowHoverStyle="true" Selecting-AllowRowSelect="true" Scrolling-FrozenColumnsCount="3"> <Scrolling AllowScroll="true" UseStaticHeaders="true" /> </ClientSettings> <GroupingSettings CaseSensitive="false" /> </telerik:RadGrid>Here is the binding:
var contracts = (from ci in db.ContractInformationViews where sites.Contains(ci.SiteId.Trim()) select new { ci.BillName, ci.Addr1, ci.ContractId, ci.SiteId, ci.CustId, ci.EffectiveDate, ci.ExpireDate, TotalAmt = ci.TotalAmt, TotalBilled = ci.TotalBilled, ci.Status, ci.Lupd_DateTime }).ToList(); Session["ContractInfo"] = contracts; rg.DataSource = contracts; rg.DataBind();Here is the export code:
rg.ExportSettings.Excel.Format = (GridExcelExportFormat)Enum.Parse(typeof(GridExcelExportFormat), "Xlsx"); rg.ExportSettings.IgnorePaging = true; rg.ExportSettings.ExportOnlyData = true; rg.ExportSettings.OpenInNewWindow = true; rg.ExportSettings.FileName = "Contract Information"; rg.MasterTableView.ExportToExcel();0
Sean
Top achievements
Rank 1
answered on 09 Dec 2015, 07:28 PM
- RESOLVED -
Converting the status from Char to String fixed it. Not sure why Excel, or your conversation process is trying to convert Chars to Doubles though.
0
Hello Sean,
By default Excel is trying to "guess" what is the content type in the cells and this can cause some errors.
If you would like to specify the format of the cells explicitly you should use the approach described in the following article:
On a side note, I noticed that you are using DataBind method to provide data for RadGrid. This method is used for simple data binding. As the name implies this type of binding is suitable only for the most simple scenarios.
In most cases it is recommended to use advanced data binding for RadGrid. You can handle the NeedDataSource event and set the DataSource for the grid in the handler.
Regards,
Viktor Tachev
Telerik
By default Excel is trying to "guess" what is the content type in the cells and this can cause some errors.
If you would like to specify the format of the cells explicitly you should use the approach described in the following article:
On a side note, I noticed that you are using DataBind method to provide data for RadGrid. This method is used for simple data binding. As the name implies this type of binding is suitable only for the most simple scenarios.
In most cases it is recommended to use advanced data binding for RadGrid. You can handle the NeedDataSource event and set the DataSource for the grid in the handler.
Regards,
Viktor Tachev
Telerik
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 Feedback Portal
and vote to affect the priority of the items
