Hi All,
I have a nested grid where I would like to export all items in the Master grid, but only those child grids that contain data.
Using this example (http://www.telerik.com/help/aspnet-ajax/grid-hide-expand-collapse-images-when-no-records.html) I thought I had the solution, but it's not working.
I'm probably doing the part in the if statement wrong (..Expanded = true/false) - what's the correct way to do this?
Thx in advance!
M.
Code in Grid_OnItemCommand:
I have a nested grid where I would like to export all items in the Master grid, but only those child grids that contain data.
Using this example (http://www.telerik.com/help/aspnet-ajax/grid-hide-expand-collapse-images-when-no-records.html) I thought I had the solution, but it's not working.
I'm probably doing the part in the if statement wrong (..Expanded = true/false) - what's the correct way to do this?
Thx in advance!
M.
Code in Grid_OnItemCommand:
if (e.CommandName.Contains("Export")) { //this expands everything // RadGrid1.MasterTableView.HierarchyDefaultExpanded = true; GridItem[] nestedViewItems = RadGrid1.MasterTableView.GetItems(GridItemType.NestedView); foreach (GridNestedViewItem nestedViewItem in nestedViewItems) { foreach (GridTableView nestedView in nestedViewItem.NestedTableViews) { if (nestedView.Items.Count == 0) { RadGrid1.MasterTableView.Items[nestedViewItem.ItemIndex].Expanded = false; } else { RadGrid1.MasterTableView.Items[nestedViewItem.ItemIndex].Expanded = true; } } } }9 Answers, 1 is accepted
0
Princy
Top achievements
Rank 2
answered on 03 Jul 2013, 04:52 AM
Hi Marcus,
Please try the below code snippet to hide records with no data and those will be ignored during export.Dont forget to set
HierarchyLoadMode="ServerBind".
ASPX:
C#:
Thanks,
Princy
Please try the below code snippet to hide records with no data and those will be ignored during export.Dont forget to set
HierarchyLoadMode="ServerBind".
ASPX:
<telerik:RadScriptManager ID="RadScriptManager1" runat="server" /><telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource2" runat="server" OnItemCommand="RadGrid1_ItemCommand" OnPreRender="RadGrid1_PreRender"> <MasterTableView DataSourceID="SqlDataSource2" DataKeyNames="OrderID" CommandItemDisplay="Top" Name="Orders" HierarchyLoadMode="ServerBind"> <CommandItemSettings ShowExportToExcelButton="true" ShowExportToWordButton="true" ShowExportToPdfButton="true"></CommandItemSettings> <DetailTables> <telerik:GridTableView Name="OrderDetails" DataKeyNames="OrderID" DataSourceID="SqlDataSource3" runat="server"> <ParentTableRelation> <telerik:GridRelationFields DetailKeyField="OrderID" MasterKeyField="OrderID"></telerik:GridRelationFields> </ParentTableRelation> <Columns> . . . . . . . . . . . . . . . . . . . . . </Columns> </telerik:GridTableView> </DetailTables> <Columns> . . . . . . . . . . . . . . . . . . . . . . </Columns> </MasterTableView> <ExportSettings ExportOnlyData="true" IgnorePaging="true" OpenInNewWindow="true"> <Excel Format="Html"></Excel> </ExportSettings></telerik:RadGrid><asp:SqlDataSource ID="SqlDataSource2" ConnectionString="<%$ ConnectionStrings:Northwind_newConnectionString3 %>" ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM Orders " runat="server"></asp:SqlDataSource><asp:SqlDataSource ID="SqlDataSource3" ConnectionString="<%$ ConnectionStrings:Northwind_newConnectionString3 %>" ProviderName="System.Data.SqlClient" SelectCommand="SELECT* FROM [Order Details] where OrderID = @OrderID" runat="server"> <SelectParameters> <asp:SessionParameter Name="OrderID" SessionField="OrderID" Type="Int32"></asp:SessionParameter> </SelectParameters></asp:SqlDataSource>C#:
protected void RadGrid1_PreRender(object sender, EventArgs e) { HideExpandColumnRecursive(RadGrid1.MasterTableView); } private bool isExport = false; protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) { if (e.CommandName.Contains("Export")) { isExport = true; RadGrid1.MasterTableView.HierarchyDefaultExpanded = true; // for the first level RadGrid1.MasterTableView.DetailTables[0].HierarchyDefaultExpanded = true; // for the second level } } public void HideExpandColumnRecursive(GridTableView tableView) { GridItem[] nestedViewItems = tableView.GetItems(GridItemType.NestedView); foreach (GridNestedViewItem nestedViewItem in nestedViewItems) { foreach (GridTableView nestedView in nestedViewItem.NestedTableViews) { if (nestedView.Items.Count == 0) { TableCell cell = nestedView.ParentItem["ExpandColumn"]; cell.Controls[0].Visible = false; cell.Text = " "; nestedViewItem.Visible = false; } if (nestedView.HasDetailTables) { HideExpandColumnRecursive(nestedView); } } } }Thanks,
Princy
0
Marcus
Top achievements
Rank 1
answered on 03 Jul 2013, 06:47 PM
Hi Princy,
thx for the reply, but that's not what I'm looking for.
I don't want to hide the expand link for empty child tables and I don't want to expand all child tables by default (I'm maintaining viewstate as described here: http://demos.telerik.com/aspnet-ajax/grid/examples/export/exporthierarchicalgrid/defaultcs.aspx).
I just want to (temporarily) expand those child grids that have data on export.
Thx&Rgds - M.
thx for the reply, but that's not what I'm looking for.
I don't want to hide the expand link for empty child tables and I don't want to expand all child tables by default (I'm maintaining viewstate as described here: http://demos.telerik.com/aspnet-ajax/grid/examples/export/exporthierarchicalgrid/defaultcs.aspx).
I just want to (temporarily) expand those child grids that have data on export.
Thx&Rgds - M.
0
Hello Marcus,
A possible approach is to expand all items and set visible false to the DetailTables which do not have a data in it. Another approach is to hook OnDataBound and loop through all MasterTableView items and set their Expanded property to true. Check out the following code snippet.
Regards,
Kostadin
Telerik
A possible approach is to expand all items and set visible false to the DetailTables which do not have a data in it. Another approach is to hook OnDataBound and loop through all MasterTableView items and set their Expanded property to true. Check out the following code snippet.
protected void RadGrid1_DataBound(object sender, EventArgs e){ if (isExport) { foreach (GridDataItem item in RadGrid1.MasterTableView.Items) { item.Expanded = true; } }}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
Marcus
Top achievements
Rank 1
answered on 05 Jul 2013, 09:52 PM
Hi Kostadin,
that's exactly what I'm trying to do - set visible or expanded to false on all detail tables that don't have any data. But the code isn't working.
The nestedViewItem.NestedTableViews collection (from my first post) is always empty.
So again, how do I loop through the MasterTable items and determine if the childgrid has data?
M.
Edit: I also tried your code (item.Expanded = true/false) in both onDataBound and onItemCommand. In onItemCommand it doesn't do anything. In onDatabound it just inserts an empty row in the export.
that's exactly what I'm trying to do - set visible or expanded to false on all detail tables that don't have any data. But the code isn't working.
The nestedViewItem.NestedTableViews collection (from my first post) is always empty.
So again, how do I loop through the MasterTable items and determine if the childgrid has data?
M.
Edit: I also tried your code (item.Expanded = true/false) in both onDataBound and onItemCommand. In onItemCommand it doesn't do anything. In onDatabound it just inserts an empty row in the export.
0
Princy
Top achievements
Rank 2
answered on 08 Jul 2013, 01:06 PM
Hi Marcus,
Can you please try the following code snippet and check if this helps.Here those rows with data will be expanded and while export it will export only rows with data.You can expand or collapse a row.If any concern, please let know.
ASPX:
C#:
Thanks,
Princy
Can you please try the following code snippet and check if this helps.Here those rows with data will be expanded and while export it will export only rows with data.You can expand or collapse a row.If any concern, please let know.
ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False"PageSize="5" AllowPaging="True" " ondatabound="RadGrid1_DataBound"><ExportSettings IgnorePaging="true" ExportOnlyData="true"> <Excel Format="Html"></Excel></ExportSettings><MasterTableView DataKeyNames="OrderID" CommandItemDisplay="Top" Name="Orders" ExpandCollapseColumn-Display="true" HierarchyLoadMode="ServerBind"> <CommandItemSettings ShowExportToExcelButton="true"></CommandItemSettings> <DetailTables> <telerik:GridTableView Name="OrderDetails" DataKeyNames="OrderID" runat="server"> <ParentTableRelation> <telerik:GridRelationFields DetailKeyField="OrderID" MasterKeyField="OrderID"></telerik:GridRelationFields> </ParentTableRelation> <Columns> . . . . . . . . . . . . . . . . </Columns> </telerik:GridTableView> </DetailTables> <Columns> . . . . . . . . . . . . .. . . . </Columns></MasterTableView></telerik:RadGrid>C#:
protected void RadGrid1_DataBound(object sender, EventArgs e) { HideExpandColumnRecursive(RadGrid1.MasterTableView); } public void HideExpandColumnRecursive(GridTableView tableView) { GridItem[] nestedViewItems = tableView.GetItems(GridItemType.NestedView); foreach (GridNestedViewItem nestedViewItem in nestedViewItems) { foreach (GridTableView nestedView in nestedViewItem.NestedTableViews) { if (nestedView.Items.Count != 0) { nestedView.ParentItem.Expanded = true; } } } }Thanks,
Princy
0
Marcus
Top achievements
Rank 1
answered on 08 Jul 2013, 06:30 PM
Princy:
please note my comment in the previous post:
<<The nestedViewItem.NestedTableViews collection (from my first post) is always empty.>>
please note my comment in the previous post:
<<The nestedViewItem.NestedTableViews collection (from my first post) is always empty.>>
0
Hello Marcus,
Could you please provide your code declaration and the related code behind? This way we will be able to give you more to the point answer.
Regards,
Kostadin
Telerik
Could you please provide your code declaration and the related code behind? This way we will be able to give you more to the point answer.
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
Marcus
Top achievements
Rank 1
answered on 12 Jul 2013, 07:52 PM
Hi Kostadin,
is there a way that I could email you the page, or upload it somewhere?
There are a couple of things in there that I would rather not post on a public forum and it's too much work to "generisize" it.
Thx for your help!
M.
is there a way that I could email you the page, or upload it somewhere?
There are a couple of things in there that I would rather not post on a public forum and it's too much work to "generisize" it.
Thx for your help!
M.
0
Hello Marcus,
You could use any public ftp server to upload your project there and provide us with a link. Additionally you could prepared a small sample with a dummy database and provide the code here in the forum.
Regards,
Kostadin
Telerik
You could use any public ftp server to upload your project there and provide us with a link. Additionally you could prepared a small sample with a dummy database and provide the code here in the forum.
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.