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

ExportToExcel with RadComboBox in RadGrid rows

1 Answer 73 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mosart
Top achievements
Rank 1
Mosart asked on 08 Jun 2012, 08:39 PM
Hello Support,

I have a RadGrid that manually exports to Excel via ItemCommand (RadGrid1.MasterTableView.ExportToExcel()). The RadGrid has a RadComboBox in every row. When I export (using ExportOnlyData = True), the RadComboBox columns are ignored and do not show in the exported Excel file.

How do you suggest to print each combo SelectedText to the excel sheet? In which event shoudl I handle it?


Thanks for your help.
Mosart

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 11 Jun 2012, 06:19 AM
Hi Mosart,

I created a sample code based on your scenario. Please test it and see if it helps.

ASPX:
<telerik:RadGrid ID="radgrid1" runat="server" DataSourceID="SqlDataSource1" OnItemCommand="radgrid1_ItemCommand"
            OnItemDataBound="radgrid1_ItemDataBound">
            <ExportSettings ExportOnlyData="false" Excel-Format="Html" />
            <MasterTableView CommandItemDisplay="Top">
                <CommandItemSettings ShowExportToExcelButton="true" />
                <Columns>
                    <telerik:GridTemplateColumn HeaderText="test" UniqueName="TempCol">
                        <ItemTemplate>
                            <telerik:RadComboBox ID="RadComboBox1" runat="server" DataSourceID="SqlDataSource1"
                                DataTextField="CategoryID" DataValueField="CategoryID" SelectedValue='<%# Eval("CategoryID") %>'>
                            </telerik:RadComboBox>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>
</telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]" />
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]" />

C#:
bool isExport = false;
protected void radgrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName.Contains("Export"))
        {
            isExport = true;
            if (!radgrid1.ExportSettings.IgnorePaging)
            {
                radgrid1.Rebind();
            }
        }
    }
protected void radgrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (isExport)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = e.Item as GridDataItem;
 
                item["TempCol"].Text = GetCellContents(item["TempCol"]);
            }
 
            if (e.Item is GridCommandItem)
            {
                e.Item.Visible = false;
            }
        }
    }
private string GetCellContents(TableCell tableCell)
    {
        string cellText = String.Empty;
        foreach (Control ctrl in tableCell.Controls)
        {
            if (ctrl is ITextControl)
            {
                if (ctrl is RadComboBox)
                {
                    cellText += (ctrl as RadComboBox).SelectedItem.Text;
                }
                else
                {
                    cellText += (ctrl as ITextControl).Text;
                }
            }
        }
        return cellText.Replace(" ", " ").Trim();
    }

Thanks,
princy.
Tags
Grid
Asked by
Mosart
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or