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

Icons and Data in Export

6 Answers 179 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Technology
Top achievements
Rank 1
Technology asked on 14 Nov 2012, 11:41 AM
Dear Telerik team,

i have a grid with some extra icon column (GridImageColumn). The value of this column is in another column from db. I make it visible = false from server code.
When i export the grid in Excel (ExportOnlyData = True), i cannot see the value of the GridImageColumn
i want to show on excel the value of the column (db column) and not the icon column.

Thank you in advance for your time.

I Use:
VS 2008 (VB)
Telerik 2011.3.1305.35

Below is the client and server code.

<telerik:RadGrid ID="grdPPCOrders" runat="server" Height="300px" Skin="Windows7"
                    AllowMultiRowSelection="true">
                    <HeaderContextMenu CssClass="">
                    </HeaderContextMenu>
                    <MasterTableView DataKeyNames="ID">
                        <Columns>
                            <telerik:GridClientSelectColumn UniqueName="Selected" HeaderStyle-Width="30">
                                <HeaderStyle Width="30px"></HeaderStyle>
                            </telerik:GridClientSelectColumn>
                            <telerik:GridImageColumn UniqueName="CardTypeImage" HeaderStyle-Width="30" >
                                <HeaderStyle Width="30px"></HeaderStyle>
                            </telerik:GridImageColumn>
                            <telerik:GridImageColumn UniqueName="OrderStatusImage" HeaderStyle-Width="30">
                                <HeaderStyle Width="30px"></HeaderStyle>
                            </telerik:GridImageColumn>
                            <telerik:GridImageColumn UniqueName="HasCreditNoteImage" HeaderStyle-Width="30">
                                <HeaderStyle Width="30px"></HeaderStyle>
                            </telerik:GridImageColumn>
                            <telerik:GridBoundColumn FilterControlWidth="105px" DataField="ID" HeaderText="ID"
                                SortExpression="ID" UniqueName="ID2" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains"
                                ShowFilterIcon="false" ItemStyle-Width="50" HeaderStyle-Width="50">
                                <HeaderStyle Width="50px"></HeaderStyle>
                                <ItemStyle Width="50px"></ItemStyle>
                            </telerik:GridBoundColumn>
                        </Columns>
                        <CommandItemSettings ShowExportToExcelButton="true" ShowAddNewRecordButton="false" />
                        <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                            <HeaderStyle Width="20px"></HeaderStyle>
                        </RowIndicatorColumn>
                        <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
                            <HeaderStyle Width="20px"></HeaderStyle>
                        </ExpandCollapseColumn>
                        <EditFormSettings>
                            <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                            </EditColumn>
                        </EditFormSettings>
                    </MasterTableView>
                    <ClientSettings AllowKeyboardNavigation="true" EnablePostBackOnRowClick="true" EnableRowHoverStyle="True">
                        <Selecting AllowRowSelect="True" EnableDragToSelectRows="False" />
                        <ClientEvents />
                        <Scrolling AllowScroll="true" UseStaticHeaders="True" SaveScrollPosition="True" />
                        <Resizing AllowColumnResize="True" AllowResizeToFit="True" EnableRealTimeResize="True" />
                    </ClientSettings>
                    <FilterMenu EnableImageSprites="False">
                    </FilterMenu>
                </telerik:RadGrid>


Server:
Protected Sub btnExportExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportExcel.Click
    grdPPCOrders.ExportSettings.ExportOnlyData = True
    'grdPPCOrders.MasterTableView.Columns.Item(0).Visible = False
    'grdPPCOrders.MasterTableView.Columns.FindByDataField("CardType").Visible = True !ERROR Cannot find column bound to field
    'grdPPCOrders.Columns.FindByDataField("CardType").Visible = True !ERROR Cannot find column bound to field
    'grdPPCOrders.MasterTableView.Columns.Item(9).Visible = True
    grdPPCOrders.MasterTableView.ExportToExcel()
End Sub


Protected Sub grdPPCOrders_ColumnCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridColumnCreatedEventArgs) Handles grdPPCOrders.ColumnCreated
 
        Select Case e.Column.UniqueName
 
            Case "Notes", "ID", "SupplierID_CustomerID", "VesselID", "OrderStatus", "CardType", "HasCreditNote", "InvoiceDate", "Price", "NetProfit"
                Dim boundColumn As GridBoundColumn = CType(e.Column, GridBoundColumn)
                boundColumn.Visible = False
 
           End Select
 
    End Sub


Protected Sub grdPPCOrders_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles grdPPCOrders.ItemDataBound
        'Dim SelectedColumn(GridColumns.Count) As String
 
        If TypeOf e.Item Is GridHeaderItem Then
            Dim header As GridHeaderItem = DirectCast(e.Item, GridHeaderItem)
            'header("OrderStatus").Text = ""
            'header("HasCreditNote").Text = ""
 
        End If
 
        If TypeOf e.Item Is GridDataItem Then
            Dim dataItem As GridDataItem = CType(e.Item, GridDataItem)
 
            Dim CardTypeColumn As String = dataItem("CardType").Text
            If CardTypeColumn = "Virtual" Then
                CType(dataItem("CardTypeImage").Controls(0), Image).ImageUrl = "~/Images/CardVirtual.png"
            ElseIf CardTypeColumn = "Scratch" Then
                CType(dataItem("CardTypeImage").Controls(0), Image).ImageUrl = "~/Images/CardScratch.png"
            ElseIf CardTypeColumn = "VirtualOP" Then
                CType(dataItem("CardTypeImage").Controls(0), Image).ImageUrl = "~/Images/CardVirtualOP.png"
            ElseIf CardTypeColumn = "ScratchOP" Then
                CType(dataItem("CardTypeImage").Controls(0), Image).ImageUrl = "~/Images/CardScratchOP.png"
            End If
 
            Dim OrderStatusColumn As String = dataItem("OrderStatus").Text
            If OrderStatusColumn = "1" Then
                CType(dataItem("OrderStatusImage").Controls(0), Image).ImageUrl = "~/Images/OrderClosed.png"
            ElseIf OrderStatusColumn = "2" Then
                CType(dataItem("OrderStatusImage").Controls(0), Image).ImageUrl = "~/Images/OrderOpen.png"
            ElseIf OrderStatusColumn = "3" Then
                CType(dataItem("OrderStatusImage").Controls(0), Image).ImageUrl = "~/Images/OrderSuspended.png"
            ElseIf OrderStatusColumn = "4" Then
                CType(dataItem("OrderStatusImage").Controls(0), Image).ImageUrl = "~/Images/OrderClosedTr.png"
            ElseIf OrderStatusColumn = "5" Then
                CType(dataItem("OrderStatusImage").Controls(0), Image).ImageUrl = "~/Images/OrderOpenTr.png"
            ElseIf OrderStatusColumn = "6" Then
                CType(dataItem("OrderStatusImage").Controls(0), Image).ImageUrl = "~/Images/OrderSuspendedTr.png"
            End If
 
            Dim HasCreditNoteColumn As String = dataItem("HasCreditNote").Text
            If HasCreditNoteColumn = "1" Then
                CType(dataItem("HasCreditNoteImage").Controls(0), Image).ImageUrl = "~/Images/CreditNoteDiscount.png"
            ElseIf HasCreditNoteColumn = "2" Then
                CType(dataItem("HasCreditNoteImage").Controls(0), Image).ImageUrl = "~/Images/CreditNoteCards.png"
            Else
                dataItem("HasCreditNoteImage").Controls(0).Visible = False
            End If
 
 
            Dim TotalChargeColumn As String = dataItem("Charge").Text
            dataItem("Charge").Text = ConvertStringToDecimal(dataItem("Charge").Text, 2)
            dataItem("TotalCharge").Text = ConvertStringToDecimal(dataItem("TotalCharge").Text, 2)
            'dataItem("Price").Text = ConvertStringToDecimal(dataItem("Price").Text, 2)
 
            dataItem("Charge").HorizontalAlign = HorizontalAlign.Right
            dataItem("TotalCharge").HorizontalAlign = HorizontalAlign.Right
            'dataItem("Price").HorizontalAlign = HorizontalAlign.Right
            dataItem("Cards").HorizontalAlign = HorizontalAlign.Right
 
 
            Charge += dataItem("TotalCharge").Text
            Cards += dataItem("Cards").Text
 
            'If INMBData <> " " Then
            '    INMBValue += CDec(INMBData)
            'End If
 
            'Dim fieldValue As Int32 = Integer.Parse(dataItem("ID").Text)
            'TotalCharge = TotalCharge + TotalChargeField
 
        End If
 
        If (TypeOf e.Item Is GridFooterItem) Then
            Dim footerItem As GridFooterItem = CType(e.Item, GridFooterItem)
 
            footerItem("TotalCharge").Text = ConvertStringToDecimal(Charge.ToString, 2)
            footerItem("TotalCharge").HorizontalAlign = HorizontalAlign.Right
            footerItem("Cards").Text = ConvertStringToDecimal(Cards, 0)
            footerItem("Cards").HorizontalAlign = HorizontalAlign.Right
 
            'footerItem("INMB~Data (1.35$ / MIN)").Text = INMBValue.ToString()
            footerItem("Vessel").Controls.Add(New LiteralControl("<span style='color: Black; font-weight: bold;'>Totals:</span> "))
        End If
 
 
    End Sub



6 Answers, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 19 Nov 2012, 09:16 AM
Hello,

A possible approach is to create a boolean variable and check whether the grid is exporting. If the grid is exporting than you could set the visible of the column to true. Check out the following code snippet.
Dim isExported As Boolean = False
Protected Sub btnExportExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportExcel.Click
    isExported = True
    grdPPCOrders.ExportSettings.ExportOnlyData = True
    'grdPPCOrders.MasterTableView.Columns.Item(0).Visible = False
    'grdPPCOrders.MasterTableView.Columns.FindByDataField("CardType").Visible = True !ERROR Cannot find column bound to field
    'grdPPCOrders.Columns.FindByDataField("CardType").Visible = True !ERROR Cannot find column bound to field
    'grdPPCOrders.MasterTableView.Columns.Item(9).Visible = True
    grdPPCOrders.MasterTableView.ExportToExcel()
End Sub

Protected Sub grdPPCOrders_ColumnCreated(sender As Object, e As Telerik.Web.UI.GridColumnCreatedEventArgs)
    If isExported Then
        Select Case e.Column.UniqueName
            Case "Notes", "ID", "SupplierID_CustomerID", "VesselID", "OrderStatus", "CardType", _
                "HasCreditNote", "InvoiceDate", "Price", "NetProfit"
                Dim boundColumn As GridBoundColumn = DirectCast(e.Column, GridBoundColumn)
                boundColumn.Visible = True
                Exit Select
 
        End Select
    Else
 
        Select Case e.Column.UniqueName
 
            Case "Notes", "ID", "SupplierID_CustomerID", "VesselID", "OrderStatus", "CardType", _
                "HasCreditNote", "InvoiceDate", "Price", "NetProfit"
                Dim boundColumn As GridBoundColumn = DirectCast(e.Column, GridBoundColumn)
                boundColumn.Visible = False
                Exit Select
 
        End Select
    End If
 
End Sub


All the best,
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
Technology
Top achievements
Rank 1
answered on 19 Nov 2012, 10:26 AM
Dear Kostadin ,

first of all thank you for your quick respond.

This is not working because the "ColumnCreated" event is trigered first and then the button_click.
So every time the IsExported is False.

Best regards,
George
Navarino Technology Department
0
Kostadin
Telerik team
answered on 21 Nov 2012, 02:44 PM
Hello George,

You could either call Rebind() or set grdPPCOrders.ExportSettings.IgnorePaging to true in the btnExportExcel_Click event handler.

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
Technology
Top achievements
Rank 1
answered on 26 Nov 2012, 01:34 PM
Dear Kostadin,

thank you for your time.
This is also not working and i think that IgnorePaging it has nothing to do with the icons and columns data.

Best regards,
George.
0
Accepted
Kostadin
Telerik team
answered on 29 Nov 2012, 01:57 PM
Hi George,

I suggested you to set IgnorePaging to true as in this case the grid will rebind and will fetch all the data from the database and will populate the grid with it. Nevertheless I realized that the suggested approach is not suitabmle in your case as the OnGridColumnCreated event handler is being called too late in the page life cycle. Another possible solution is to hide/show the columns OnItemCreated event handler.
For your convenience I prepared a small example and attached it to this forum post. Give it a try and let me know whether this approach proves helpful.

All the best,
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
Technology
Top achievements
Rank 1
answered on 29 Nov 2012, 03:12 PM
Dear Kostadin,

first of all, thank you for your time.

It is working now.
With "RadGrid1.MasterTableView.GetColumn("CustomerID").Visible = isExported;" from "RadGrid1_ItemCreated" and only with "NeedDataSource" event for the data.

Also the function "GetCellByColumnName" is very usefull.

Thank you again for your help.

Best Regards,
George.
Tags
Grid
Asked by
Technology
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
Technology
Top achievements
Rank 1
Share this question
or