Radgrid export problem

3 posts, 1 answers
  1. deepak
    deepak avatar
    22 posts
    Member since:
    Dec 2010

    Posted 14 Jan 2013 Link to this post

    Hi,

     I have got a radgrid and i am trying to export the grid to excel as ExcelML format.But i have a template column for which the data is biniding from radgrid item data bound event.While exporting i am not getting this template column. I have used he following code to export

    rdtg.ExportSettings.IgnorePaging = true;
                    rdtg.ExportSettings.FileName = "List";
                    rdtg.ExportSettings.Excel.Format = Telerik.Web.UI.GridExcelExportFormat.ExcelML;
                    rdtg.ExportSettings.ExportOnlyData = true;
                    rdtg.ExportSettings.OpenInNewWindow = true;                
                    rdtg.MasterTableView.ExportToExcel();

    How can i export the this template column whose data is binding from item data bound event?
  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 14 Jan 2013 Link to this post

    Hi deepak,

    With reference to this forum thread, RadGrid ExcelML export does not export template columns since it gets the data directly from the datasource and ignores non-data contents. Following is the sample code that I tried.

    ASPX:
    <telerik:RadGrid ID="RadGrid1"OnExcelMLExportRowCreated="RadGrid1_ExcelMLExportRowCreated" runat="server" onitemdatabound="RadGrid1_ItemDataBound"  .............. >
         <MasterTableView>
              <Columns>
                   ..............
                   <telerik:GridTemplateColumn HeaderText="Name" UniqueName="Name" >
                      <ItemTemplate>
                         <asp:Label ID="Label1" runat="server"></asp:Label>
                      </ItemTemplate>
                   </telerik:GridTemplateColumn>
              </Columns>
         </MasterTableView>
     </telerik:RadGrid>

    C#:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem DataItem = (GridDataItem)e.Item;
            Label lbl = (Label)DataItem.FindControl("Label1");
            lbl.Text = "Your text";
     
        }
    }
    protected void RadGrid1_ExcelMLExportRowCreated(object sender, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowCreatedArgs e)
    {
        if (e.RowType == GridExportExcelMLRowType.HeaderRow)
        {
            RadGrid1.Rebind();  //workaround to get the template column's content
     
            //create new column element and a header cell
            e.Worksheet.Table.Columns.Add(new ColumnElement());
            CellElement cell = new CellElement();
     
            //correct the autofilter
            e.Worksheet.AutoFilter.Range = String.Format("R{0}C{1}:R{0}C{2}", 1, 1, e.Worksheet.Table.Columns.Count + 1);
     
            //populate the header cell
            cell.Data.DataItem = (RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0] as GridHeaderItem)["Name"].Text.Trim();
            e.Row.Cells.Add(cell);
        }
     
        if (e.RowType == GridExportExcelMLRowType.DataRow)
        {
            //create cell for the current row
            CellElement cell = new CellElement();
            int currentRow = e.Worksheet.Table.Rows.IndexOf(e.Row) - 1;
     
            //populate the data cell
            Label lbl = (Label)RadGrid1.MasterTableView.Items[currentRow].FindControl("Label1");
            cell.Data.DataItem = lbl.Text;
            e.Row.Cells.Add(cell);
        }
    }

    Hope this helps.

    Regards,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. deepak
    deepak avatar
    22 posts
    Member since:
    Dec 2010

    Posted 15 Jan 2013 Link to this post

    Thanks 
Back to Top