I need to export a datatable to Excel. I have a cosume control inheridet from RadPageView. basically is a TAB with other control on it. I made a Radgid dynamically like the following:
// I include also the grid to my Tab
div.Controls.AddAt(0, grid); // Div a div container in this TAB. by running this code i can see my grid with all columns and rows
but if i add the exportSetting, and export it doesn't show the grid (What actually I want), and no Excel file neither the code that I'm using is:
Any help would be greatly appreciated.
RadGrid grid =
new
RadGrid { AutoGenerateColumns =
true
};
grid.DataSource = dt.DefaultView;
// DT is a DataTable
grid.DataBind();
div.Controls.AddAt(0, grid); // Div a div container in this TAB. by running this code i can see my grid with all columns and rows
but if i add the exportSetting, and export it doesn't show the grid (What actually I want), and no Excel file neither the code that I'm using is:
grid.ExportSettings.OpenInNewWindow =
true
grid.ExportSettings.FileName = "CSV Writer
grid.MasterTableView.GridLines = GridLines.Both
grid.MasterTableView.ShowHeader =
true
grid.MasterTableView.ExportToCSV();
Any help would be greatly appreciated.
8 Answers, 1 is accepted
0

Princy
Top achievements
Rank 2
answered on 16 Feb 2011, 08:26 AM
Hello Emmanuel,
Can you please try the same code using Grid / Advanced Data Binding for your grid? You can attach NeeddataSource event to RadGrid and try to attach DataSource to Radgrid in that event.
C#:
Thanks,
Princy.
Can you please try the same code using Grid / Advanced Data Binding for your grid? You can attach NeeddataSource event to RadGrid and try to attach DataSource to Radgrid in that event.
C#:
grid.NeedDataSource +=
new
GridNeedDataSourceEventHandler(grid_NeedDataSource);
protected
void
grid_NeedDataSource(
object
source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
grid.DataSource = dt.DefaultView;
}
Thanks,
Princy.
0

Emmanuel
Top achievements
Rank 1
answered on 17 Feb 2011, 03:00 AM
Thank you for the reply, but it didn't fix my issue. The problem is i try in a composite control otherwise i have already made in a normal form without any problems. I tried another way by making a new control inherited from RadPageView and showing my grid. in this control i added a Hyperlink and add the following javascript in the page for onclick event of the HL:
i made the grid with the following attributes
the javascript is in a external File, and i can see the page html code and I have a div with Exportgrid as ID, but still the javascript code cannot find the grid, and I get the error null is null. Another point I am using Jquery in my project. Is Telerik Javascript library is loaded automatically by making any controls, or should I include some script file? Thank you again, and I really appreciated your help
function ExportToCSV() {
var grid = $find('Exportgrid');
var masterTable = grid.get_masterTableView();
masterTable.exportToExcel();
return false;
};
RadGrid grid = new RadGrid { AutoGenerateColumns = false, ClientIDMode= System.Web.UI.ClientIDMode.Static, ID = "Exportgrid"
the javascript is in a external File, and i can see the page html code and I have a div with Exportgrid as ID, but still the javascript code cannot find the grid, and I get the error null is null. Another point I am using Jquery in my project. Is Telerik Javascript library is loaded automatically by making any controls, or should I include some script file? Thank you again, and I really appreciated your help
0

Emmanuel
Top achievements
Rank 1
answered on 17 Feb 2011, 08:57 PM
Just an update to the error. in javascript
Does get_masterTableView is getting information from Server? in this case I can create again the grid and bind it to my initial DataTable. I am open to any suggestion Server side or Client Side, Thanks again for your help
Update: I added an OnGridCreated event in order to get the maaterTableView when the grid is created
But the function
var grid = $find('Exportgrid'); // return a grid object
var masterTable = grid.get_masterTableView(); // but mastertTable is null consequently can't be exported
Does get_masterTableView is getting information from Server? in this case I can create again the grid and bind it to my initial DataTable. I am open to any suggestion Server side or Client Side, Thanks again for your help
Update: I added an OnGridCreated event in order to get the maaterTableView when the grid is created
grid.ClientSettings.ClientEvents.OnGridCreated =
"OnExportGridCreated"
;
var grid, masterTable;
function OnExportGridCreated(sender, eventArgs) {
alert('Created');
grid = sender;
masterTable = grid.get_masterTableView();
}
OnExportGridCreated
is never be called. I changed the name to OnExportGridCreated
1 on server side and i am getting the error OnExportGridCreated
1 doesn't exist. Should I add some more properties in order to get the On GridCreated event? TIA0
Hello Emmanuel,
Please try the following:
If the problem still persist please attach a runnable version (submit a support ticket) of your project and I will debug it locally.
Best regards,
Daniel
the Telerik team
Please try the following:
<script type=
"text/javascript"
>
function
pageLoad()
{
var
grid = $find(
'<%= Exportgrid.ClientID %>'
);
var
masterTable = grid.get_masterTableView();
}
If the problem still persist please attach a runnable version (submit a support ticket) of your project and I will debug it locally.
Best regards,
Daniel
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0

Emmanuel
Top achievements
Rank 1
answered on 23 Feb 2011, 01:56 AM
Thank you Daniel, but same think Grid is here, but MasterTable is null again. I can't send you a workable code because is a really complicated solution, and we're making custom control that inherit from so many other stuff that is really impossible to make a workable module outside of our environment. I attached the code associated to the part that o use the rad grid. maybe you can see some error, or might give me some other direction. thanks again.
void
CreateMainControl()
{
HtmlGenericControl divMain =
new
HtmlGenericControl { ID =
"divMain"
, TagName =
"div"
};
divMain.Style.Add(
"width"
,
"100%"
);
Table tbl =
new
Table();
TableRow tr =
new
TableRow();
TableCell tc =
new
TableCell { VerticalAlign = VerticalAlign.Middle, Height = Unit.Pixel(32) };
if
(strCSV.Length > 0)
{
tc.Controls.Add(
new
CheckBox { ID =
"chkHeaderExcel"
, Text =
"Header"
, TextAlign = TextAlign.Right, Checked =
true
});
HyperLink hr =
new
HyperLink { Width = Unit.Pixel(32), Height = Unit.Pixel(32), CssClass =
"ExportToExcel"
, ToolTip =
"Export to Excel"
, ID =
"btnExportToExcel"
};
tc.Controls.Add(
new
LiteralControl(
" "
));
tc.Controls.Add(hr);
tr.Cells.Add(tc);
tbl.Rows.Add(tr);
tc =
new
TableCell();
tr =
new
TableRow();
List<
int
> lstFieldSelectedIndic =
new
List<
int
>();
string
[] divs = strCSV.Split(
';'
);
foreach
(
string
strdiv
in
divs)
{
string
[] items = strdiv.Split(
','
);
foreach
(
string
item
in
items)
{
string
[] couple = item.Split(
'|'
);
switch
(couple[0])
{
case
"indice"
:
lstFieldSelectedIndic.Add(Convert.ToInt32(couple[1]));
break
;
}
}
}
HtmlGenericControl divGrid =
new
HtmlGenericControl { TagName =
"div"
, ID =
"divGrid"
};
divGrid.Style.Add(
"overflow"
,
"auto"
);
using
(grid =
new
RadGrid { AutoGenerateColumns =
false
, ClientIDMode = System.Web.UI.ClientIDMode.Static, ID =
"Exportgrid"
})
{
grid.MasterTableView.EnableViewState =
false
;
grid.ClientSettings.Scrolling.AllowScroll =
true
;
grid.ClientSettings.Scrolling.UseStaticHeaders =
true
;
grid.ClientSettings.ClientEvents.OnGridCreated =
"OnExportGridCreated"
;
grid.ClientSettings.ClientEvents.OnCommand =
"OnExportGridCommand"
;
grid.NeedDataSource +=
new
GridNeedDataSourceEventHandler(grid_NeedDataSource);
grid.Rebind();
grid.HeaderStyle.Width = Unit.Pixel(50);
GridTableView gtv = grid.MasterTableView;
gtv.Width = Unit.Percentage(100);
gtv.GridLines = GridLines.Vertical;
List<field> lstField = Get_Table_Column();
foreach
(
int
ind
in
lstFieldSelectedIndic)
{
gtv.Columns.Add(
new
GridBoundColumn { DataField = lstField[ind].Field, HeaderText = lstField[ind].Field });
}
divGrid.Controls.Add(grid);
}
tc.Controls.Add(divGrid);
tr.Cells.Add(tc);
tbl.Rows.Add(tr);
divMain.Controls.Add(tbl);
//-------------------- Export
grid.ExportSettings.OpenInNewWindow =
true
;
grid.ExportSettings.FileName =
"CSV Writer"
;
grid.MasterTableView.GridLines = GridLines.Both;
grid.MasterTableView.ShowHeader =
true
;
grid.MasterTableView.ExportToCSV();
//-----------------------------------
}
this
.Controls.Add(divMain);
}
void
grid_NeedDataSource(
object
sender, GridNeedDataSourceEventArgs e)
{
try
{
RadGrid grid = sender
as
RadGrid;
if
(grid !=
null
)
{
using
(var service = ServiceClients.GetReportClient(_phoenixUser.UserId, _phoenixUser.Password))
{
MemoryStream ms =
null
;
SqlStatementAndReportWriter sqlStatmentAndReportWriter = ReportBodyTab.BuildSqlStatement();
using
(ms = service.Service.GetDataTableFromSQLString(DateTime.Now, _phoenixUser.ClientCode,
sqlStatmentAndReportWriter.SqlStatement,
sqlStatmentAndReportWriter.ReportWriterClass.ListOfSQLParameters.ToArray()))
{
if
(ms ==
null
|| ms.Length == 0)
{
ms.Close();
}
DataContractSerializer ser =
new
DataContractSerializer(
typeof
(DataTable));
DataTable dt = (DataTable)ser.ReadObject(ms);
grid.DataSource = dt.DefaultView;
}
}
}
}
catch
(Exception ex)
{
throw
ex;
}
}
0
Hello Emmanuel,
Thanks for the sample code.
Where do you run the CreateMainControl method from? Could you please check whether the MasterTableView object is created properly? You can use Firebug console for that purpose.
Regards,
Daniel
the Telerik team
Thanks for the sample code.
Where do you run the CreateMainControl method from? Could you please check whether the MasterTableView object is created properly? You can use Firebug console for that purpose.
> $telerik.findControl(document,
"Exportgrid"
).get_masterTableView();
Regards,
Daniel
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0

Emmanuel
Top achievements
Rank 1
answered on 28 Feb 2011, 11:30 PM
Daniel,
CreateMainControl is called from CreateChildControls of my compositeControl. In which event should I use your java code? in PageLoad? or some client event of the grid? TIA
CreateMainControl is called from CreateChildControls of my compositeControl. In which event should I use your java code? in PageLoad? or some client event of the grid? TIA
0
Hello Emmanuel,
Please open your website in Mozilla Firefox and press F12 to open Firebug console.
In the debug console, enter "
Under normal circumstances this will return the MasterTableView object - as shown on the attached screenshot.
Regards,
Daniel
the Telerik team
Please open your website in Mozilla Firefox and press F12 to open Firebug console.
In the debug console, enter "
$telerik.findControl(document,
"Exportgrid"
).get_masterTableView();
"Under normal circumstances this will return the MasterTableView object - as shown on the attached screenshot.
Regards,
Daniel
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!