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

Export to CSV and GridTemplateColumn

16 Answers 235 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 2
Richard asked on 10 Sep 2008, 08:34 PM
I'm using RadGrid with object datasource. I'm using telerik:GridTemplateColumn to display Gender (GenderCode = 1 --> GenderCodeText = male, GenderCode = 2 --> GenderCodeText = female):
<telerik:GridTemplateColumn DataField="GenderCode" DataType="System.Byte" SortExpression="GenderCodeText"
    HeaderText="Gender" UniqueName="Gender">
    <ItemTemplate>
        <asp:Label ID="LblGender" runat="server" Text='<%# Eval("GenderCodeText") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <telerik:RadComboBox ID="CboGender" runat="server" SelectedValue='<%# Eval( "GenderCode") %>'
            DataSourceID="OdsGenders" DataTextField="Description" DataValueField="Code">
        </telerik:RadComboBox>
    </EditItemTemplate>
</telerik:GridTemplateColumn>

If I export grid values to CSV, all values for Gender are exported as empty string. If I use ExportToExcel (or ExportToWord) instead of ExportToCSV, everything works OK.

Could you help me please?

     Richard

16 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 11 Sep 2008, 05:25 AM
Hi Ricahrd,

One suggestion would be to add a GridBoundColumn bind it with the same data as the GridTemplateColumn and initially hide the column. On the click event of the export button hide the Template column and show the GridBoundColumn.

    Go through the following help article to get more details.
   Exporting tips and tricks

Thanks
Shinu.
0
Richard
Top achievements
Rank 2
answered on 11 Sep 2008, 07:47 AM
It helps me.

 Thanks.

     Richard
0
Martin Roussel
Top achievements
Rank 1
answered on 05 Feb 2013, 03:27 PM
Hi, im having similar CSV exporting problem with GridTemplateColumns with Q3 2012 (1205). It seems to occur when ExportOnlyData="true". Is the solution provided still the way of doing it? Seems to be annoying to duplicate columns all the time and im confident a better solution exists. Im asking since answer is kinda old now.

I tried adding a duplicate GridBoundColumn (Visible="False") for each GridTemplateColumn and setting column visibility in the OnItemCommand event:
protected void RadGrid1_OnItemCommand(object o, Telerik.Web.UI.GridCommandEventArgs e)
        {
           
                if (e.CommandName == RadGrid.ExportToExcelCommandName || e.CommandName == RadGrid.ExportToPdfCommandName || e.CommandName == RadGrid.ExportToCsvCommandName || e.CommandName == RadGrid.ExportToWordCommandName)
                {
                    RadGrid1.MasterTableView.Columns.FindByUniqueName("columnBound").Visible = true;
                    RadGrid1.MasterTableView.Columns.FindByUniqueName("columnTemplate").Visible = false;
 
                }
               
        }

It succeeds to hide the GridTemplateColumn in the export but not show the GridBoundColumn. What can I do wrong?


TIA

Martin
0
Shinu
Top achievements
Rank 1
answered on 06 Feb 2013, 04:52 AM
Hi,

The BoundColumn will not be rendered in browser mode when Visible property is set as "false". In order to hide the column, use the Display property by which the columns can be accessed from code behind. Here is the sample code.
aspx:
<telerik:GridBoundColumn Display="false"  DataField="OrderID"UniqueName="OrderID"></telerik:GridBoundColumn>
C#:
  protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
         if (e.CommandName == RadGrid.ExportToExcelCommandName)
        {
            RadGrid1.MasterTableView.GetColumn("OrderID").Display = true;
        }
}

Thanks,
Shinu
0
Martin Roussel
Top achievements
Rank 1
answered on 06 Feb 2013, 12:50 PM
Thanks Shinu, "Display" seems the way to go!

I still need some clarifications:

1-Is this CSV export/GridTemplateColumns/ExportOnlyData="true" behavior (empty strings) known and expected?

2-Is the GridBoundColumn workaround you mentioned above (in 2008) still the way to solve this?

3-I have issues when I have an hierarchical grid and want to hide a specific column in the export of the DetailTable. If doesnt generate any error but I end up with duplicate columns, like the hiding didnt work. Tried with both visible and display properties:
if (e.CommandName == RadGrid.ExportToExcelCommandName || e.CommandName == RadGrid.ExportToPdfCommandName || e.CommandName == RadGrid.ExportToCsvCommandName || e.CommandName == RadGrid.ExportToWordCommandName)
                {
  RadGrid1.MasterTableView.DetailTables[0].Columns.FindByUniqueName("columnBound").Display = true;
  RadGrid1.MasterTableView.DetailTables[0].Columns.FindByUniqueName("columnTemplate").Visible = false;
  RadGrid1.MasterTableView.DetailTables[0].Columns.FindByUniqueName("columnTemplate").Display = false;
 
}

Can you please help?

TIA

Martin


0
Shinu
Top achievements
Rank 1
answered on 07 Feb 2013, 06:26 AM
Hi,

While setting ExportOnlyData="true", the contents (text) of all template columns will be removed but the columns will be still visible in the exported file. In order to hide the columns in detailtable, try the following code.
C#:
protected void grid_ItemCommand(object sender, GridCommandEventArgs e)
{
        if (e.CommandName == RadGrid.ExportToExcelCommandName && e.Item.OwnerTableView.Name == "DetailTable1")
        {
            GridTableView detailTable = (GridTableView)e.Item.OwnerTableView;
            detailTable.GetColumn("Uniquename").Visible = false;
        }
}

Thanks,
Shinu
0
Martin Roussel
Top achievements
Rank 1
answered on 07 Feb 2013, 12:47 PM
Shinu, it doesnt work.

When I do the export, the ItemCommand is fired once and e.Item.OwnerTableView.Name = "Master" (name of my master table). Obviously, it wont enter in the if statement.

Also, can you please confirm my question #2?

Martin


0
Kostadin
Telerik team
answered on 12 Feb 2013, 12:22 PM
Hi Martin,

Shinu suggestion to hide the TemplateColumn and show the BoundColumn is currently the way to solve this issue. If you do not want to hide and show those columns you could set ExportOnlyData to false and in this case the TemplateColumn will be exported correctly.

Greetings,
Kostadin
the Telerik team
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 their blog feed now.
0
Martin Roussel
Top achievements
Rank 1
answered on 12 Feb 2013, 12:33 PM
thanks for the clarification Kostadin. Will wait for reply about DetailTable column hiding.

TIA

Martin
0
Shinu
Top achievements
Rank 1
answered on 13 Feb 2013, 05:22 AM
Hi,

Try setting the Name property for DetailTable as shown below.
aspx:
<telerik:GridTableView DataSourceID="SqlDataSource2" Name="DetailTable1"    runat="server"AutoGenerateColumns="False"></telerik:GridTableView>

Thanks,
Shinu
0
Martin Roussel
Top achievements
Rank 1
answered on 13 Feb 2013, 12:54 PM
Hi Shinu,

My DetailTable already has a Name defined. Here's my complete RadGrid definition:
<telerik:RadGrid ID="RadGrid3" runat="server" ClientSettings-EnableRowHoverStyle="true" PageSize="20" AllowPaging="false" Width="99.9%"
                                AllowSorting="True" AllowFilteringByColumn="false" AllowMultiRowSelection="false"
                                ShowGroupPanel="false" GridLines="None" ShowFooter="false" ShowHeader="true" GroupingEnabled="false"
                                Skin="WebBlue" EnableHeaderContextAggregatesMenu="false" EnableHeaderContextMenu ="false"
                                AutoGenerateColumns="false" EnableViewState="True" OnHTMLExporting="RadGrid3_HTMLExporting"
                                OnItemCommand="RadGrid3_OnItemCommand" OnDetailTableDataBind="RadGrid3_DetailTableDataBind" OnNeedDataSource="RadGrid3_NeedDataSource"
                                OnItemDataBound="RadGrid3_OnItemDataBound" OnGroupsChanging="RadGrid3_OnGroupsChanging" OnColumnsReorder="RadGrid3_OnColumnsReorder">
                             
                                <MasterTableView HierarchyLoadMode="Client" Name="Master"
                                 CommandItemDisplay="Top" CommandItemSettings-ShowAddNewRecordButton="false"
                                 CommandItemSettings-ShowExportToExcelButton="true" CommandItemSettings-ShowExportToWordButton="true"
                                 CommandItemSettings-ShowExportToPdfButton="true" CommandItemSettings-ShowExportToCsvButton="true" AllowMultiColumnSorting="true"
                                 Caption="">
                                  
 
                                     <Columns>
 
                                            <telerik:GridBoundColumn HeaderText="Relation" meta:resourcekey="labRelation" UniqueName="strRelation" HeaderStyle-Width="50%" Resizable="true" Reorderable="true" SortExpression="strRelation" DataField="strRelation">
                                            </telerik:GridBoundColumn>
 
                                            <telerik:GridBoundColumn HeaderText="Description" meta:resourcekey="labDescription" UniqueName="strDescription" HeaderStyle-Width="50%" Resizable="true" Reorderable="true" SortExpression="strDescription" DataField="strDescription">
                                            </telerik:GridBoundColumn>
                                        
                                      </Columns
 
                                            <DetailTables>
                                                <telerik:GridTableView Name="DetailTable1" runat="server" AutoGenerateColumns="False" >
                                                    <Columns>
 
                                                        <telerik:GridBoundColumn HeaderText="" UniqueName="strField" HeaderStyle-Width="25%" ItemStyle-CssClass="GridFieldColumn" Resizable="true" Reorderable="true" DataField="strField">
                                                        </telerik:GridBoundColumn>
 
                                                        <telerik:GridBoundColumn Display="false" HeaderText="" UniqueName="strValueForExport" DataField="strValueForExport">
                                                        </telerik:GridBoundColumn>
 
                                                        <telerik:GridTemplateColumn HeaderText="" UniqueName="strValue" HeaderStyle-Width="75%" Resizable="true" Reorderable="true">
                                                            <ItemTemplate>
                                                              <%# Eval("strValue")%>
                                                              <asp:HyperLink ID="hl" Target="_blank" runat="server" OnClick="CloseTooltip()" Text='<%# Eval("strLinkResource")%>' NavigateUrl='<%# Eval("strLinkValue")%>'></asp:HyperLink>
                                                            </ItemTemplate>
                                                        </telerik:GridTemplateColumn>
                                                    </Columns>  
                                                </telerik:GridTableView>
                                            </DetailTables>
 
                                </MasterTableView>
 
                                <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
 
                                        <ClientSettings EnablePostBackOnRowClick="False" AllowRowsDragDrop="false" AllowDragToGroup="false" AllowColumnsReorder="false" ReorderColumnsOnClient="false" ColumnsReorderMethod="Reorder">
                                            <Resizing AllowRowResize="False" EnableRealTimeResize="True" ResizeGridOnColumnResize="false" AllowColumnResize="true" AllowResizeToFit="true"/>
                                            <Animation AllowColumnReorderAnimation="false" AllowColumnRevertAnimation="true" />
                                            <Selecting AllowRowSelect="true"  />
                                            <ClientEvents OnRowClick="OnRowClick"></ClientEvents>
                                        </ClientSettings>
 
                                        <SortingSettings SortedBackColor="Azure" EnableSkinSortStyles="false" />
 
                               <GroupingSettings ShowUnGroupButton="true" />
 
                               <ExportSettings OpenInNewWindow="true" HideStructureColumns="true" ExportOnlyData="true">
                                </ExportSettings>
 
                            </telerik:RadGrid>

As I previously reported, when I try to export in Excel, RadGrid3_OnItemCommand() is triggered once and e.Item.OwnerTableView.Name == "Master". Attachment is provided to illustrate.

Probably something is not right in the RadGrid definition since my OnItemCommand() doesnt seem to work like yours.

TIA

Martin
0
Kostadin
Telerik team
answered on 15 Feb 2013, 10:22 AM
Hi,

You could use the following approach to hide the chosen column:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    RadGrid1.MasterTableView.DetailTables[0].GetColumn("DetailColumnUniqueName").Visible = false;       
}


Regards,
Kostadin
the Telerik team
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 their blog feed now.
0
Martin Roussel
Top achievements
Rank 1
answered on 15 Feb 2013, 01:24 PM
Kostadin,

Ive already tried that and unfortunately the column end up being exported. I even tried it on all DetailTable columns and every one of them are exported. No error is reported by VS (so it finds the column). I also tried Display="false" and same thing.

TIA

Martin
0
Kostadin
Telerik team
answered on 20 Feb 2013, 11:43 AM
Hello Martin,

In this case you have to loop through all cells and set their visible properties to false. For your convenience I prepared a small sample and attached it to this forum post. Please give it a try and let me know about the result.

Kind regards,
Kostadin
the Telerik team
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 their blog feed now.
0
Martin Roussel
Top achievements
Rank 1
answered on 20 Feb 2013, 01:39 PM
Kostadin,

this finally hides the column! However, it unfortunately breaks the solution you provided me in this ticket for the "after-tooltip" problem (setting Expanded property to false to all collapsed items). When I expand a row and use an child tooltip then hides back that row, it ends in the exported file. I think that both solution are conflicting each other. Here is my ItemCommand function:

void RadGrid4_ItemCommand(object sender, GridCommandEventArgs e)
       {
 
           isExport = true;
           PersistExpandedItems(RadGrid4.MasterTableView);
 
 
           if (e.CommandName.IndexOf("CustomExport") == 0)
           {
               string expArg = e.CommandArgument.ToString();
 
               if (!String.IsNullOrEmpty(expArg))
               {
                   string[] itemIndexes = expArg.Split(',');
                   foreach (GridDataItem dataItem in RadGrid4.MasterTableView.Items)
                   {
                       if (itemIndexes.Contains(dataItem.ItemIndex.ToString()))
                       {
                           dataItem.Expanded = true;
                       }
                       else
                       {
                           dataItem.Expanded = false;
                       }
                   }
               }
               else
               {
                   foreach (GridDataItem item in RadGrid4.MasterTableView.Items)
                   {
                       item.Expanded = false;
                   }
               }
 
 
               switch (e.CommandName)
               {
                   case "Custom" + RadGrid.ExportToCsvCommandName: RadGrid4.MasterTableView.ExportToCSV(); break;
                   case "Custom" + RadGrid.ExportToPdfCommandName: RadGrid4.MasterTableView.ExportToPdf(); break;
                   case "Custom" + RadGrid.ExportToWordCommandName: RadGrid4.MasterTableView.ExportToWord(); break;
                   case "Custom" + RadGrid.ExportToExcelCommandName: RadGrid4.MasterTableView.ExportToExcel(); break;
               }
           }
 
 
 
       }

TIA

Martin
0
Kostadin
Telerik team
answered on 22 Feb 2013, 02:53 PM
Hi Martin,

As far as I can see those two approaches should not get in conflict to each other. Could you please open a formal support ticket and provide us with a runnable project? I will be glad to help you solve this issue.

Regards,
Kostadin
the Telerik team
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 their blog feed now.
Tags
Grid
Asked by
Richard
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 1
Richard
Top achievements
Rank 2
Martin Roussel
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or