I have a RadGrid which is created at runtime and added to a place holder control.
I set the commandItemSettings.ShowExportToExcelButton = true and the built-in export button appears.
After the grid is created I call the Rebind() method so that my NeedDataSource event is raised (Advanced Data Binding):
I'm using NeedDataSource event:
void radGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
this.radGrid.DataSource = this.dataSource;
}
When I click the Export to Excel button nothing happens.
How should I implement this or use it so that the export works?
Many thanks,
I set the commandItemSettings.ShowExportToExcelButton = true and the built-in export button appears.
After the grid is created I call the Rebind() method so that my NeedDataSource event is raised (Advanced Data Binding):
I'm using NeedDataSource event:
void radGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
this.radGrid.DataSource = this.dataSource;
}
When I click the Export to Excel button nothing happens.
How should I implement this or use it so that the export works?
Many thanks,
7 Answers, 1 is accepted
0
Accepted

Shinu
Top achievements
Rank 2
answered on 16 Jun 2011, 09:55 AM
Hello Pooya,
If you are using AdvannedDataBinding using NeedDataSource Event, No need to explicitly call the Rebind method after creating the Grid. The event will fire automatically when the Grid needs data. However I tried with and without Rebind. Both worked as expected.Here is the code that I tried.
C#:
Have you ajaxified the Grid? The exporting feature of the control work with regular postbacks only. So please make sure that you have disabled ajax before exporting.
Check out the following help documentation for more on this.
Export from ajaxified grid.
Thanks,
Shinu.
If you are using AdvannedDataBinding using NeedDataSource Event, No need to explicitly call the Rebind method after creating the Grid. The event will fire automatically when the Grid needs data. However I tried with and without Rebind. Both worked as expected.Here is the code that I tried.
C#:
protected
void
Page_Load(
object
sender, EventArgs e)
{
RadGrid RadGrid1 =
new
RadGrid();
RadGrid1.ID =
"RadGrid1"
;
PlaceHolder1.Controls.Add(RadGrid1);
if
(!IsPostBack)
{
RadGrid1.MasterTableView.DataKeyNames =
new
string
[] {
"EmployeeID"
};
RadGrid1.AllowPaging =
true
;
RadGrid1.MasterTableView.AutoGenerateColumns =
false
;
RadGrid1.MasterTableView.CommandItemSettings.ShowExportToExcelButton =
true
;
RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
RadGrid1.NeedDataSource +=
new
GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
RadGrid1.PageSize = 15;
GridBoundColumn boundColumn;
boundColumn =
new
GridBoundColumn();
RadGrid1.MasterTableView.Columns.Add(boundColumn);
boundColumn.DataField =
"EmployeeID"
;
boundColumn.HeaderText =
"EmployeeID"
;
boundColumn =
new
GridBoundColumn();
RadGrid1.MasterTableView.Columns.Add(boundColumn);
boundColumn.DataField =
"LastName"
;
boundColumn.HeaderText =
"LastName"
;
//RadGrid1.Rebind();no need to call this.
}
}
void
RadGrid1_NeedDataSource(
object
sender, GridNeedDataSourceEventArgs e)
{
RadGrid rdd = sender
as
RadGrid;
rdd.DataSource = BindGrid();
}
protected
DataTable BindGrid()
{
SqlConnection con =
new
SqlConnection(WebConfigurationManager.ConnectionStrings[
"NorthwindConnectionStringEmp"
].ConnectionString);
SqlCommand cmd1 =
new
SqlCommand(
"select * from Employees"
, con);
SqlDataAdapter ad1 =
new
SqlDataAdapter(cmd1);
DataTable ds1 =
new
DataTable();
ad1.Fill(ds1);
return
ds1;
}
Have you ajaxified the Grid? The exporting feature of the control work with regular postbacks only. So please make sure that you have disabled ajax before exporting.
Check out the following help documentation for more on this.
Export from ajaxified grid.
Thanks,
Shinu.
0

Pooya
Top achievements
Rank 1
answered on 16 Jun 2011, 10:03 AM
Many thanks, it worked like a charm with ajax disabled!
but I'd need to enable ajax.
but I'd need to enable ajax.
0

Pooya
Top achievements
Rank 1
answered on 16 Jun 2011, 10:04 AM
Just when it exports the data as Excel or PDF, it adds an extra ugly line on top with few button-like controls.
How to get rid of that in the export? is there any property that I should be setting?
I have already set ExportSettings.HideStructureColumns = true;
Thanks,
How to get rid of that in the export? is there any property that I should be setting?
I have already set ExportSettings.HideStructureColumns = true;
Thanks,
0

Princy
Top achievements
Rank 2
answered on 16 Jun 2011, 10:39 AM
Hello Pooya,
Try setting ExportOnlyData as true which exports only data.
RadGrid1.ExportSettings.ExportOnlyData = true;
Thanks,
Princy.
Try setting ExportOnlyData as true which exports only data.
RadGrid1.ExportSettings.ExportOnlyData = true;
Thanks,
Princy.
0

Pooya
Top achievements
Rank 1
answered on 16 Jun 2011, 10:48 AM
Thanks,,
I wrote this in the radgrid_ItemCommand method and it worked:
foreach (var commandItem in this.radGrid.MasterTableView.GetItems(GridItemType.CommandItem))
{
commandItem.Visible = false;
}
Export to Word and Excel work fine. But Export to Pdf doesn't export template columns.
Any idea?
I wrote this in the radgrid_ItemCommand method and it worked:
foreach (var commandItem in this.radGrid.MasterTableView.GetItems(GridItemType.CommandItem))
{
commandItem.Visible = false;
}
Export to Word and Excel work fine. But Export to Pdf doesn't export template columns.
Any idea?
0

Shinu
Top achievements
Rank 2
answered on 17 Jun 2011, 06:02 AM
Hello Pooya,
I cannot reproduce the issue locally. It worked as expected. Here is the code that I tried to export.
C#:
Also check out the following demo which also implements the same functionality.
Grid / Export to PDF.
Thanks,
Shinu.
I cannot reproduce the issue locally. It worked as expected. Here is the code that I tried to export.
C#:
protected
void
Button2_Click(
object
sender, EventArgs e)
{
RadGrid1.ExportSettings.ExportOnlyData =
true
;
RadGrid1.ExportSettings.IgnorePaging =
true
;
RadGrid1.ExportSettings.OpenInNewWindow =
true
;
RadGrid1.MasterTableView.ExportToPdf();
}
Also check out the following demo which also implements the same functionality.
Grid / Export to PDF.
Thanks,
Shinu.
0

Pooya
Top achievements
Rank 1
answered on 17 Jun 2011, 03:13 PM
I used the code suggested there to register the control using ScriptManager:
void _radGrid_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridCommandItem)
{
var exportToExcelButton = (e.Item as GridCommandItem).FindControl("ExportToExcelButton") as Button;
PageUtil.RegisterPostBackControl(exportToExcelButton);
var exportToWordButton = (e.Item as GridCommandItem).FindControl("ExportToWordButton") as Button;
PageUtil.RegisterPostBackControl(exportToWordButton);
var exportToPdfButton = (e.Item as GridCommandItem).FindControl("ExportToPdfButton") as Button;
PageUtil.RegisterPostBackControl(exportToPdfButton);
}
}
public static void RegisterPostBackControl(Control control)
{
var currentPage = (Page) HttpContext.Current.CurrentHandler;
var currentScriptManager = ScriptManager.GetCurrent(currentPage);
if (currentScriptManager != null)
{
currentScriptManager.RegisterPostBackControl(control);
}
}
When the Excel button is clicked for example, a popup window opens. When I click Open, it says there is no file to open. When I click Save, it saves the file properly on the disk and then I can open it from there.
Why Open doesn't work in the first place? that's odd. Any idea/clues?
Thanks,
void _radGrid_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridCommandItem)
{
var exportToExcelButton = (e.Item as GridCommandItem).FindControl("ExportToExcelButton") as Button;
PageUtil.RegisterPostBackControl(exportToExcelButton);
var exportToWordButton = (e.Item as GridCommandItem).FindControl("ExportToWordButton") as Button;
PageUtil.RegisterPostBackControl(exportToWordButton);
var exportToPdfButton = (e.Item as GridCommandItem).FindControl("ExportToPdfButton") as Button;
PageUtil.RegisterPostBackControl(exportToPdfButton);
}
}
public static void RegisterPostBackControl(Control control)
{
var currentPage = (Page) HttpContext.Current.CurrentHandler;
var currentScriptManager = ScriptManager.GetCurrent(currentPage);
if (currentScriptManager != null)
{
currentScriptManager.RegisterPostBackControl(control);
}
}
When the Excel button is clicked for example, a popup window opens. When I click Open, it says there is no file to open. When I click Save, it saves the file properly on the disk and then I can open it from there.
Why Open doesn't work in the first place? that's odd. Any idea/clues?
Thanks,