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

[Solved] Export to Excel Configuration

2 Answers 104 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Neil
Top achievements
Rank 1
Neil asked on 16 Jul 2013, 01:37 PM
Hi,
Here I have 2 problems ( one relatively simple, one sticky)

1 - The small "export to Excel" button in CommandItemSettings (ShowExportToExcelButton="true") it doesn't export anything, just refreshes the grid ( not the page) and makes the command line disappear, any ideas ? I also have a button export to excel which call the radgrid.export to excel command and exports the grid to excel. So I gues sIm missing some setting.

2 ( the sticky one) now the above grid configuration has a sqldatasource assigned to it. Ideally When the page is loaded for the first time I would like to display the grid but with no data-source ( basically it wont be visible) and then after some user input and doing a post back I go:

Grid.DataSource = result;
Grid.DataBind();

And the grid is built. But then when I try to export to excel I get an empty excel sheet, and after some testing it has to do something with not having that sql outsource initially. is there a way around this ?
regards, Neil

2 Answers, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 19 Jul 2013, 08:05 AM
Hello Neil,

Thank you for contacting us.

About the first issue I assume that the grid is ajaxified and you did not disable the ajax when export command is being fired. As to the second one I would recommend you to use advanced data binding. The key to the advanced data binding of a RadGrid control is handling the NeedDataSource event. So you could initially pass an empty DataTable as data source and after a postback you could populate the grid as you prefer. Keep in mind that you have to call Rebind()  method if you want to populate the grid. The following code snippet demonstrates how the aforementioned suggestion could be implemented and additionally I called Rebind() on button click. 
protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        if (!IsPostBack)
        {
            RadGrid1.DataSource = new DataTable();
        }
        else
        {
            DataTable table = new DataTable();
            table.Columns.Add("Column1");
            table.Columns.Add("Column2");
            table.Columns.Add("Column3");
            table.Columns.Add("Column4");
 
            for (int i = 0; i < 10; i++)
            {
                table.Rows.Add("Col1Row" + i, "Col2Row" + i, "Col3Row" + i, "Col4Row" + i);
            }
 
            RadGrid1.DataSource = table;
        }
    }
 
    protected void Button1_Click(object sender, EventArgs e)
    {
        RadGrid1.Rebind();
    }


I hope this information helps.

Regards,
Kostadin
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Neil
Top achievements
Rank 1
answered on 19 Jul 2013, 08:07 AM
Yeah need datasource event is what I needed. Thanks a lot !
Tags
Grid
Asked by
Neil
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
Neil
Top achievements
Rank 1
Share this question
or