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

Export to Excel with RadGrid bound to datatable

4 Answers 331 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Prithvi
Top achievements
Rank 1
Prithvi asked on 02 Feb 2011, 06:10 PM
Hi,

I am using the RadGrid to export the data to excel. I have a button the same page. When I click the button, I set the following in the click event in the server

rgInvoice.ExportSettings.ExportOnlyData = true;
rgInvoice.ExportSettings.IgnorePaging = true;
rgInvoice.ExportSettings.OpenInNewWindow = true;
rgInvoice.MasterTableView.ExportToExcel();

I am following the example in this link
http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/exporting/defaultcs.aspx
The only difference is that I am binding a datatable to the RadGrid in the .cs file.

The export works when I click the header CommandItem in the RadGrid. But when I click the button in the page, the export CommandItems header with the buttons disappear and the export doesn't work. Can anyone suggest what might be wrong.

Thanks
Prithvi

4 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 02 Feb 2011, 10:39 PM
Hello Prithvi,

It seems that your export button is ajaxfied. Please examine the following help topic for more information:
Export from ajaxified grid

I hope this helps.

Best regards,
Daniel
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Prithvi
Top achievements
Rank 1
answered on 03 Feb 2011, 11:21 AM
Hi Daniel,

I've already done that. Here is the code.

<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">
            function onRequestStart(sender, args) {
                if (args.get_eventTarget().indexOf("rbtnSaveToExcel") >= 0) {
                    args.set_enableAjax(false);
                }
            }
        </script>
    </telerik:RadCodeBlock>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <ClientEvents OnRequestStart="onRequestStart" />
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="rgInvoice">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="rgInvoice" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>

<telerik:RadButton ID="rbtnSaveToExcel" runat="server" Text="Save To Excel" OnClick="rbtnSaveToExcel_Click" CausesValidation="False">



<telerik:RadGrid ID="rgInvoice" runat="server" AutoGenerateColumns="False" GridLines="None"
                        OnItemDataBound="rgInvoice_ItemDataBound"
                        OnNeedDataSource="rgInvoice_NeedDataSource">
                        <ExportSettings ExportOnlyData="True" HideStructureColumns="true">
                        </ExportSettings>
                        <MasterTableView CommandItemDisplay="Top">
                            <CommandItemSettings ShowExportToWordButton="false" ShowExportToExcelButton="true"
                                ShowExportToCsvButton="false" ShowAddNewRecordButton="false" ShowRefreshButton="false" />
                            <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                                <HeaderStyle Width="20px" />
                            </RowIndicatorColumn>
                            <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
                                <HeaderStyle Width="20px" />
                            </ExpandCollapseColumn>
                            <Columns>
                                <telerik:GridBoundColumn DataField="MediaID" FilterControlAltText="Filter MediaID column"
                                    HeaderText="MediaID" UniqueName="MediaID" Visible="False">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="MediaTitle" FilterControlAltText="Filter MediaTitle column"
                                    HeaderText="Media Title" UniqueName="MediaTitle">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="Edit" FilterControlAltText="Filter Edit column"
                                    HeaderText="Edit" UniqueName="Edit">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="Runtime" FilterControlAltText="Filter Runtime column"
                                    HeaderText="Minutes" UniqueName="Runtime">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="JCA" FilterControlAltText="Filter JCA column"
                                    HeaderText="JCA" UniqueName="JCA">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="RuntimeOther" FilterControlAltText="Filter RuntimeOther column"
                                    HeaderText="Other RunTime" UniqueName="RuntimeOther" Visible="False">
                                </telerik:GridBoundColumn>
                            </Columns>
                            <EditFormSettings>
                                <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                                </EditColumn>
                            </EditFormSettings>
                        </MasterTableView>
                        <FilterMenu EnableImageSprites="False">
                        </FilterMenu>
                        <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
                        </HeaderContextMenu>
                    </telerik:RadGrid>

protected void rbtnSaveToExcel_Click(object sender, EventArgs e)
        {
            rgInvoice.ExportSettings.ExportOnlyData = true;
            rgInvoice.ExportSettings.IgnorePaging = true;
            rgInvoice.ExportSettings.OpenInNewWindow = true;
            rgInvoice.MasterTableView.ExportToExcel();
        }

protected void BindInvoiceGrid(short ClientID)
        {
            Media objMedia = new Media();
            rgInvoice.DataSource = objMedia.fnSelectBookedInClientMedia(ClientID); //Returns List<Media>
        }

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindInvoiceGrid(shortClientID);
            }
        }

Thanks
Prithvi
0
Prithvi
Top achievements
Rank 1
answered on 04 Feb 2011, 12:24 PM
Is there anyone who can help with this please..

Prithvi
0
Daniel
Telerik team
answered on 09 Feb 2011, 12:01 AM
Hello Prithvi,

These snippets work flawlessly on my end. I suppose that your code resides in user control or content page which is ajaxfied by its parent.

I also noticed that you are using simple data-binding. Note that this will cause problems when RadGrid needs to rebind itself - for example when IgnorePaging is set to true. This is why I strongly recommend that you use advanced data-binding instead.
Advanced Data-binding help topic
Advanced Data-binding demo

Best regards,
Daniel
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
Prithvi
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Prithvi
Top achievements
Rank 1
Share this question
or