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

[Solved] Export the data from GridTableView in to Excel

3 Answers 371 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Vasikar Paulraj
Top achievements
Rank 1
Vasikar Paulraj asked on 23 Nov 2009, 12:46 PM
Hi:

I have RadGrid which contains GridTableView as rows inside <DetailTables>. One of the column in RadGrid contains the image button. My requirement is whenever the Image Button from the row is clicked i need to export the data from the respective GridTableView in to Excel Sheet. I am able to export the entire grid data in to excel (tatSummaryGrd.MasterTableView.ExportToExcel()) but NOT able to export the row wise data(GridTableView data) in to Excel.

Please find the code below and advise how i can achieve this.

<radG:RadGrid ID="tatSummaryGrd"
                        runat="server" AllowMultiRowSelection="False" AllowPaging="False"
                        Width="100%" AllowSorting="True" AutoGenerateColumns="False" BorderColor="LightBlue"
                        BorderWidth="1px" AllowMultiRowEdit="true" EnableAJAX="True" GridLines="None"
                        OnDetailTableDataBind="tatSummaryGrd_DetailTableDataBind"
                        OnNeedDataSource="tatSummaryGrd_NeedDataSource"
                        OnItemCommand = "tatSummaryGrd_ItemCommand"
                        Skin="SkeltaGrid" RadControlsDir="<%$ ReplaceTemplateExpn:telerik/radcontrols/%>"
                        >
                    <mastertableview nodetailrecordstext="No records found " nomasterrecordstext="No records found"
                        autogeneratecolumns="false"  DataSourceID=""
                        datakeynames="ImportID" HierarchyDefaultExpanded="false">                       
                        <Columns>
                            <radG:GridTemplateColumn UniqueName="Process" >
                                <HeaderTemplate>
                                    Process
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <label>
                                        <%# Eval("Process")%>
                                    </label>
                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Left"  />
                                <ItemStyle HorizontalAlign="Left"  />
                            </radG:GridTemplateColumn>
                            <radG:GridTemplateColumn UniqueName="SubProcess" >
                                <HeaderTemplate>
                                    Sub Process
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <label>
                                        <%# Eval("SubProcess")%>
                                    </label>
                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Left"  />
                                <ItemStyle HorizontalAlign="Left"  />
                            </radG:GridTemplateColumn>                           
                            <radG:GridTemplateColumn UniqueName="ExportToExcel" >
                                <HeaderTemplate>
                                    Export to Excel                                  
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:ImageButton ID="exportToExcelImgBtn" runat="server" CommandName="Excel" ImageUrl="../skimages/msexcel.jpg"
                                ToolTip="Export to Excel" />                                
                                </ItemTemplate>

                                <HeaderStyle HorizontalAlign="Left"  />
                                <ItemStyle HorizontalAlign="Left"  />
                            </radG:GridTemplateColumn>
                        </Columns>
                       
                        <DetailTables>
                            <radG:GridTableView Name="Policies" DataKeyNames="ImportID,PolicyID,InternalPolicyID,ExecutionId" AllowMultiColumnSorting="True"
                                    DataSourceID="" TabIndex="0" AllowAutomaticUpdates="true"
                                    >
                                <ParentTableRelation>
                                    <radG:GridRelationFields MasterKeyField="ImportID" DetailKeyField="ImportID" />
                                 </ParentTableRelation>
                                 <Columns>
                                    <radG:GridTemplateColumn UniqueName="PolicyID">
                                        <HeaderTemplate>
                                            Policy
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <label >
                                                <%# Eval("PolicyID")%>
                                            </label>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="left" Width="90px" />
                                        <ItemStyle HorizontalAlign="Left" Width="90px" />
                                    </radG:GridTemplateColumn>
                                    <radG:GridTemplateColumn UniqueName="ExistsInExcel">
                                        <HeaderTemplate>
                                            Money Recieved
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <%# Eval("ExistsInExcel")%>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Center" Width="90px" />
                                        <ItemStyle HorizontalAlign="Center" Width="90px" />
                                    </radG:GridTemplateColumn>                                                                       
                                </Columns>
                            </radG:GridTableView>
                         </DetailTables>
                    </mastertableview>
                    
                    <exportsettings exportonlydata="true" filename="Work Item Status Report" ignorepaging="true" openinnewwindow="true">
                    </exportsettings>
                   
                </radG:RadGrid>

Code Behind:

protected

 

void tatSummaryGrd_NeedDataSource(object source, GridNeedDataSourceEventArgs e) {

 

tatSummaryGrd.DataSource = InsuranceProcessLib.GetExcelImportHistory(_FromDate, _ToDate);

}

 

protected void tatSummaryGrd_DetailTableDataBind(object source, Telerik.WebControls.GridDetailTableDataBindEventArgs e) {

 

 

 

if (_DataViews.Contains(e.DetailTableView)) return;

 

_DataViews.Add(e.DetailTableView);

 

int importID = BPM.IsNullInt(e.DetailTableView.ParentItem.GetDataKeyValue("ImportID"), 0);

 

 

e.DetailTableView.DataSource = InsuranceProcessLib.GetExcelImportPolicyList(importID);

e.DetailTableView.DataBind();

}

 

protected void tatSummaryGrd_ItemCommand(object source, GridCommandEventArgs e) {

 

 

if (e.CommandName == "Excel") {

 

 

Int32 index = Convert.ToInt32(e.CommandArgument);

 

 

GridDataItem item = tatSummaryGrd.Items[index];

 

item.OwnerTableView.ExportToExcel();

}
}

 

 

 

 

}

 




3 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 23 Nov 2009, 02:44 PM
Hello Vasikar,

The following code-snippets highlight how to get a reference to the GridTableView object in such scenario:

<rad:GridTemplateColumn>
    <ItemTemplate>
        <asp:Button ID="Button1" runat="server" Text="Export TableView" OnClick="ExportTableView" />
    </ItemTemplate>
</rad:GridTemplateColumn>

protected void ExportTableView(object sender, EventArgs e)
{
    GridTableView table = ((sender as Button).NamingContainer as GridItem).OwnerTableView;
    table.ExportToPdf();
}

Regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Vasikar Paulraj
Top achievements
Rank 1
answered on 24 Nov 2009, 07:11 AM
Hi Daniel,
Thanks for your reply. I have tried using the same code as you mentioned but the page is getting refreshed when i click on the button but the file download box is NOT coming to export to PDF file. What could be the reason for that?

Also i tried with my requirement but that also NOT working, only the page is getting refreshed as i mentioned above, the file download box is NOT coming to export to excel.
Let me re explain my requirement which would help you to provide the exact solution for the problem.

Requirement: I need to export the data from each row of the grid(GridTableView) in to Excel. I have one image button in the last column of each row. Whenever that image is clicked then the respective row data needs to be exported in to Excel.

Please find below the code i tried for my requirement and suggest how i can proceed further on this issue.

<

 

radG:GridTemplateColumn UniqueName="ExportToExcel" >

 

 

 

<HeaderTemplate>

 

 

Export to Excel

 

</HeaderTemplate>

 

 

 

<ItemTemplate>

 

 

 

<asp:ImageButton ID="exportToExcelImgBtn" runat="server" ImageUrl="../skimages/msexcel.jpg" OnClick="ExportToExcel"

 

 

 

ToolTip="Export to Excel" />

 

 

 

</ItemTemplate>

 

 

 

<HeaderStyle HorizontalAlign="Left" />

 

 

 

<ItemStyle HorizontalAlign="Left" />

 

 

 

</radG:GridTemplateColumn>

 

 


protected

 

void ExportToExcel(object sender, EventArgs e) {

 

 

 

GridTableView table = ((sender as ImageButton).NamingContainer as GridItem).OwnerTableView;

 

table.ExportToExcel();

}

 




0
Daniel
Telerik team
answered on 26 Nov 2009, 05:06 PM
Hello Vasikar,

Please examine the following links:
Export with AJAX enabled help topic
Code library project

Regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Vasikar Paulraj
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Vasikar Paulraj
Top achievements
Rank 1
Share this question
or