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

Error with RadGrid export when bound to empty dataset

3 Answers 132 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Oleg Yaroshevych
Top achievements
Rank 1
Oleg Yaroshevych asked on 02 Jul 2008, 11:08 AM
Hello,

I use ExportToExcel feature of RadGrid. I use business objects as data source, and if no data is available, export fails. This occurs only if paging is enabled in the ObjectDataSource. Please check my code. Should I create a support ticket?

Best regards, Oleg.

Windows XP SP2
RadGrid For ASP.NET AJAX 2008.1.619.35
Visual Studio 2008
.NET 3.5


Default.aspx.cs
using System;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
      RadGrid1.MasterTableView.ExportToExcel();
    }
}


Class1.cs

public class Class1
{
public Transaction[] GetData(int offset, int count)
{
return new Transaction[0];
}

public int SelectCount()
{
return 0;
}
}

public class Transaction
{
public string Name { get; set; }
public int Amount { get; set; }
}

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" DataSourceID="ObjectDataSource1"
GridLines="None">
<ExportSettings ExportOnlyData="True" IgnorePaging="True">
</ExportSettings>
<MasterTableView AutoGenerateColumns="False" DataSourceID="ObjectDataSource1">
<RowIndicatorColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
<ExpandCollapseColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
<Columns>
<telerik:GridBoundColumn DataField="Name" HeaderText="Name" SortExpression="Name"
UniqueName="Name">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Amount" DataType="System.Int32" HeaderText="Amount"
SortExpression="Amount" UniqueName="Amount">
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetData"
EnablePaging="true" SelectCountMethod="SelectCount" StartRowIndexParameterName="offset"
MaximumRowsParameterName="count" TypeName="Class1"></asp:ObjectDataSource>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>
</body>
</html>

3 Answers, 1 is accepted

Sort by
0
Bodevain Svensson
Top achievements
Rank 1
answered on 07 Jul 2008, 08:07 AM
Oleg, let me know whether my assumption does not makes sense in your case but exporting an empty grid is rather pointless. Hence you can wrap the invocation of the ExportToExcel() method inside a conditional block:

protected void Button1_Click(object sender, EventArgs e)
{
   if(RadGrid1.MasterTableView.Items > 0)
  {
      RadGrid1.MasterTableView.ExportToExcel();
  }
}


If there is no data available, display a client alert dialog when clicking the button to inform the end user that the grid should be populated in order to export it in Excel.

Bodevain 
0
Oleg Yaroshevych
Top achievements
Rank 1
answered on 07 Jul 2008, 10:01 AM
I am already using this workaround, but I believe that Telerik library shouldn't throw an exception in this case. FYI, in case if paging is disabled in ObjectDataSource, radgrid exports empty Excel file just with headers.

Best regards, Oleg.
0
Accepted
Rosen
Telerik team
answered on 07 Jul 2008, 02:15 PM
Hi Oleg,

Indeed this issue can be observed in the current version of the control. I'm happy to inform you though that our developers have addressed it and the fix will be included in next official update. As a token of gratitude for reporting this I have updated your telerik points. 

All the best,
Rosen
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
Grid
Asked by
Oleg Yaroshevych
Top achievements
Rank 1
Answers by
Bodevain Svensson
Top achievements
Rank 1
Oleg Yaroshevych
Top achievements
Rank 1
Rosen
Telerik team
Share this question
or