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

bind and export from code behind?

8 Answers 490 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steven
Top achievements
Rank 1
Steven asked on 02 Mar 2012, 11:38 PM
is it possible to export like so? I am getting an error RadGrid must be databound before exporting.

I tried moving the export code to the onDataBound 
        ReportData.DataBound += new EventHandler(ReportData_DataBound);
but it is still giving me the same error even though I can see that the grid has 2394 items.

thanks
RadGrid ReportData = new RadGrid();
 
ReportData.DataSource = datasource
ReportData.DataBind();
 
ReportData.ExportSettings.OpenInNewWindow = true;
ReportData.ExportSettings.ExportOnlyData = true;
ReportData.ExportSettings.IgnorePaging = true;
ReportData.ExportSettings.FileName = ReportName.Replace(" ", "_") + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Year.ToString();
ReportData.MasterTableView.ExportToExcel();

8 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 03 Mar 2012, 06:38 AM
Hello Steven,


protected void Page_Load(object sender, EventArgs e)
    {
        RadGrid RadGrid1 = new RadGrid();
        RadGrid1.ID = "RadGrid1";
        RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
 
        RadGrid1.AutoGenerateColumns = false;
        RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
        RadGrid1.MasterTableView.CommandItemSettings.ShowExportToExcelButton = true;
         
 
        GridBoundColumn boundColumn;
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Shipper";
        boundColumn.HeaderText = "Shipper";
        RadGrid1.MasterTableView.Columns.Add(boundColumn);
 
        this.form1.Controls.Add(RadGrid1);
    }
 
    void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
 
        DataTable dt = new DataTable();
        dt.Columns.Add("Shipper", typeof(string));
        dt.Columns.Add("ShipDate", typeof(DateTime));
        dt.Rows.Add("Shipper1", DateTime.Now.AddDays(1));
        dt.Rows.Add("Shipper2", DateTime.Now.AddDays(2));
        dt.Rows.Add("Shipper3", DateTime.Now.AddDays(3));
        dt.Rows.Add("Shipper1", DateTime.Now.AddDays(1));
        dt.Rows.Add("Shipper2", DateTime.Now.AddDays(2));
        dt.Rows.Add("Shipper3", DateTime.Now.AddDays(3));
 
        (sender as RadGrid).DataSource = dt;
 
    }

Let me know if any concern.

Thanks,
Jayesh Goyani
0
Jayesh Goyani
Top achievements
Rank 2
answered on 03 Mar 2012, 06:42 AM
Hello Steven,

protected void Page_Load(object sender, EventArgs e)
    {
        RadGrid RadGrid1 = new RadGrid();
        RadGrid1.ID = "RadGrid1";
        RadGrid1.ItemCommand += new GridCommandEventHandler(RadGrid1_ItemCommand);
 
        RadGrid1.AutoGenerateColumns = false;
        RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
        RadGrid1.MasterTableView.CommandItemSettings.ShowExportToExcelButton = true;
         
 
        GridBoundColumn boundColumn;
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Shipper";
        boundColumn.HeaderText = "Shipper";
        RadGrid1.MasterTableView.Columns.Add(boundColumn);
 
        DataTable dt = new DataTable();
        dt.Columns.Add("Shipper", typeof(string));
        dt.Columns.Add("ShipDate", typeof(DateTime));
        dt.Rows.Add("Shipper1", DateTime.Now.AddDays(1));
        dt.Rows.Add("Shipper2", DateTime.Now.AddDays(2));
        dt.Rows.Add("Shipper3", DateTime.Now.AddDays(3));
        dt.Rows.Add("Shipper1", DateTime.Now.AddDays(1));
        dt.Rows.Add("Shipper2", DateTime.Now.AddDays(2));
        dt.Rows.Add("Shipper3", DateTime.Now.AddDays(3));
 
        RadGrid1.DataSource = dt;
 
        this.form1.Controls.Add(RadGrid1);
    }
 
    void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.ExportToExcelCommandName)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Shipper", typeof(string));
            dt.Columns.Add("ShipDate", typeof(DateTime));
            dt.Rows.Add("Shipper1", DateTime.Now.AddDays(1));
            dt.Rows.Add("Shipper2", DateTime.Now.AddDays(2));
            dt.Rows.Add("Shipper3", DateTime.Now.AddDays(3));
            dt.Rows.Add("Shipper1", DateTime.Now.AddDays(1));
            dt.Rows.Add("Shipper2", DateTime.Now.AddDays(2));
            dt.Rows.Add("Shipper3", DateTime.Now.AddDays(3));
 
            (sender as RadGrid).DataSource = dt;
            (sender as RadGrid).DataBind();
 
        }
    }


Thanks,
Jayesh Goyani
0
Steven
Top achievements
Rank 1
answered on 05 Mar 2012, 06:14 PM
Looks like your example first adds the grid to  the page and then the user does something to initiate the download.

I am trying to do the bind and export in one event. Thats why I didn't add my grid to the page, I just wanted to leverage its ability to export the data for me. I don't plan on my user ever seeing a bound radgrid.

Is something like this possible?
public void Button_Onlick(object sender, EventArgs e)
{
    //bind a radgrid in code behind
    //export the radgrid to excel when its done binding
}

0
Princy
Top achievements
Rank 2
answered on 06 Mar 2012, 11:34 AM
Hello,

Try the following code.
C#:
protected void Button1_Click(object sender, EventArgs e)
{
 RadGrid RadGrid1 = new RadGrid();
 RadGrid1.ID = "RadGrid1";
 RadGrid1.AutoGenerateColumns = false;
 GridBoundColumn boundColumn;
 boundColumn = new GridBoundColumn();
 boundColumn.DataField = "Shipper";
 boundColumn.HeaderText = "Shipper";
 RadGrid1.MasterTableView.Columns.Add(boundColumn);
 DataTable dt = new DataTable();   
 //populate DataTable here
 RadGrid1.DataSource = dt;
 this.form1.Controls.Add(RadGrid1);
 RadGrid1.ExportSettings.OpenInNewWindow = true;
 RadGrid1.ExportSettings.ExportOnlyData = true;
 RadGrid1.ExportSettings.IgnorePaging = true;
 RadGrid1.MasterTableView.ExportToExcel();
}

Thanks,
Princy.
0
MBEN
Top achievements
Rank 2
Veteran
answered on 03 Apr 2017, 10:57 PM

I have a similar scenario, where I have a HTML chart but I need to export the chart data. I am using the grid merely for export.

I followed the above steps and populated the grid but nothing happens and there is no exported file.

Is there anything else that I need to do.

0
Eyup
Telerik team
answered on 06 Apr 2017, 10:43 AM
Hello,

You can try using the NeedDataSource event handler to bind the grid:
http://www.telerik.com/help/aspnet-ajax/grid-advanced-data-binding.html

Alternatively, you can create the grid declaratively in the aspx page and set its Visible property to false. On button click event handler set it back to true and export the content.

Regards,
Eyup
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
MBEN
Top achievements
Rank 2
Veteran
answered on 12 Apr 2017, 06:08 PM
<telerik:RadAjaxManagerProxy ID="RadAJAXManagerProxy1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="_btnCalculate">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="_btnCalculate" LoadingPanelID="RadAjaxLoadingPanel1" />
                    <telerik:AjaxUpdatedControl ControlID="_balance" />
                    <telerik:AjaxUpdatedControl ControlID="_balanceDate" />
                    <telerik:AjaxUpdatedControl ControlID="_totalCashFlow" />
                    <telerik:AjaxUpdatedControl ControlID="pnlChart" />
                    <%--<telerik:AjaxUpdatedControl ControlID="btnPdfExport" />
                    <telerik:AjaxUpdatedControl ControlID="btnExportToExcel" />--%>
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="rcbAccounts">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="rcbAccounts" LoadingPanelID="RadAjaxLoadingPanel1" />
                    <telerik:AjaxUpdatedControl ControlID="_balance" />
                    <telerik:AjaxUpdatedControl ControlID="_balanceDate" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManagerProxy>
 
//Code for calculation goes here
 
<asp:Panel ID="pnlChart" runat="server" Visible="false">
                <div><asp:ImageButton ID="btnPdfExport" runat="server" ImageUrl="~/App_Themes/Images/pdf_icon.gif" OnClick="btnPdfExport_Click"  />
            <asp:ImageButton ID="btnExportToExcel" ImageUrl="~/App_Themes/Images/ExportToExcel.gif" runat="server" OnClick="btnExcelExport_Click"  />
                    <telerik:RadGrid ID="rgChart" runat="server"  EnableViewState="false"
                OnPreRender="grid_PreRender" Visible="false" OnNeedDataSource="rgChart_NeedDataSource"
                 OnInfrastructureExporting="grid_InfrastructureExporting" >
                <MasterTableView TableLayout="Fixed" HierarchyDefaultExpanded="true" CommandItemDisplay="Top"
                    Name="Chart" EnableNoRecordsTemplate="true" DataKeyNames="">
                    <CommandItemSettings ShowAddNewRecordButton="false" ShowRefreshButton="false" ShowExportToExcelButton="true"
                        ExportToExcelText=""  ExportToPdfText="" ShowExportToPdfButton="true"   />
                    <Columns>
                        <telerik:GridBoundColumn HeaderText="Year" DataField="Yr" UniqueName="Yr"
                            SortExpression="Yr" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn Aggregate="Sum" DataField="CashFlow" HeaderText="CashFlow" DataFormatString="{0:c2}"
                            UniqueName="CashFlow" SortExpression="CashFlow" HeaderStyle-Width="10%" HeaderStyle-Wrap="true"
                            FooterStyle-HorizontalAlign="Right">
                        </telerik:GridBoundColumn>
                    </Columns>
                </MasterTableView>
                <ExportSettings ExportOnlyData="true" IgnorePaging="true" FileName="Activity" OpenInNewWindow="true" SuppressColumnDataFormatStrings="true">
                    <Pdf PaperSize="A4" PageLeftMargin="5px" PageRightMargin="5px" PageWidth="297mm"
                        PageHeight="210mm" PageHeader-MiddleCell-TextAlign="Center" />
                    <Excel Format="Biff" />
                </ExportSettings>
            </telerik:RadGrid>
                </div>
                <h3 class="panel_box_header">Estimated Cash Flow</h3>
                <div class="panel_contents">
                    <!--Telerik Chart-->
                    <iSys:radChart ID="barChart" runat="server" Visible="false" />
                </div>
 
            </asp:Panel>

I created the grid declaratively and set it's Visible property to false.

In my scenario, I show the chart and the export buttons on the click of a calculate button. I added the export buttons to my UpdatedControl Setting as shown in the code. However, only one of my buttons (out of the pdf and excel export) work. I believe it has something to do with the AJAX but I am unable to figure it out.

 

.cs:

protected void btnExcelExport_Click(object sender, EventArgs e)
        {
            ExportToExcel();
        }
 
        protected void btnPdfExport_Click(object sender, EventArgs e)
        {
            rgChart.Visible = true;
            rgChart.MasterTableView.ExportToPdf();
            rgChart.Visible = false;
        }
 
        protected void rgChart_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            rgChart.DataSource = CashFlow;
        }
        protected void ExportToExcel()
        {
            rgChart.Visible = true;
            rgChart.MasterTableView.ExportToExcel();
            rgChart.Visible = false;
        }

What am i missing?
0
Eyup
Telerik team
answered on 17 Apr 2017, 01:42 PM
Hi,

You should disable AJAX when exporting. This is explained in the following article:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/how-to/Exporting/export-from-ajaxified-grid

Regards,
Eyup
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Steven
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Steven
Top achievements
Rank 1
Princy
Top achievements
Rank 2
MBEN
Top achievements
Rank 2
Veteran
Eyup
Telerik team
Share this question
or