export radgrid

8 posts, 0 answers
  1. Lisa
    Lisa avatar
    43 posts
    Member since:
    Jul 2012

    Posted 17 Jul 2012 Link to this post

    1) I export radgrid to PDF fine but one column is not wide enough in PDF output. How can I increase the column size before it actually exports the data to PDF?

    2) One column's values are shown as hyperlink in PDF as well as in Excel. How can I tell it to export the values as text as opposed to links when exporting the radgrid to PDF and to Excel?

    Thank you.
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 18 Jul 2012 Link to this post

    Hello Lisa,

    You can change the column width in ItemCommand event and set the text of LinkButton in ExportCellFormatting event.
    C#:
    protected void RadGrid1_ExportCellFormatting(object sender, ExportCellFormattingEventArgs e)
    {
      GridDataItem item = e.Cell.Parent as GridDataItem;
      if (e.FormattedColumn.UniqueName == "ColumnUniqueName")
      {
        TableCell cell = item["ColumnUniqueName"];
        LinkButton link = (LinkButton)item.FindControl("LinkButton1");
       cell.Text = link.Text;
      }
    }
    protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
      if(e.CommandName==RadGrid.ExportToPdfCommandName)
     {
       RadGrid1.MasterTableView.GetColumn("UniqueName").HeaderStyle.Width = Unit.Pixel(300);//for changing column width.
     }
    }

    Thanks,
    Shinu.
  3. Lisa
    Lisa avatar
    43 posts
    Member since:
    Jul 2012

    Posted 18 Jul 2012 Link to this post

    Hello Shinu,
    Thank you for your input. Is it possible to change the font size when exporting to PDF?
    I try this and it doesn't change the font size in PDF.
                  radGrid1.MasterTableView.HeaderStyle.Font.Size = 1
    I wonder if that is because PDF allows you to change the percentage of page display and font size is automatically adjusted accordingly.

    Does any one have input for this?
    2) One column's values are shown as hyperlink in PDF as well as in Excel. How can I tell it to export the values as text as opposed to links when exporting the radgrid to PDF and to Excel?

  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 19 Jul 2012 Link to this post

    Hello,

    Try the following code to change font size when exporting to pdf.
    C#:
    bool isExport = false;
    protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
     isExport = true;
      if (e.Item is GridDataItem && isExport)
      {
        GridDataItem item = (GridDataItem)e.Item;
        foreach (TableCell cell in e.Item.Cells)
        {
         cell.Style["font-size"] = "20px"; //changing font size
        }
      }
    }
    protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
      if (e.CommandName == RadGrid.ExportToPdfCommandName)
      {
       isExport = true;
        foreach (GridDataItem item in RadGrid1.Items)
        {
           LinkButton link = (LinkButton)item.FindControl("LinkButton1");
           item["ColumnUniqueName"].Text = link.Text;//changing linkbutton when exporting
        }
        RadGrid1.MasterTableView.ExportToPdf();
      }
    }
    Also take a look at the following help documentation.
    Word/Excel Export (HTML-Based).

    Thanks,
    Shinu.
  5. Lisa
    Lisa avatar
    43 posts
    Member since:
    Jul 2012

    Posted 19 Jul 2012 Link to this post

     use ImageButton and when the button is clicked it executes the event handler which takes System.EventArgs as one of the arguments. It doesn't take ExportCellFormattingEventArgs as an argument. So how can I make your example fit in here with the ImageButton? Thanks.
  6. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 20 Jul 2012 Link to this post

    Hi,

    Try handling the click event as shown below.
    C#:
    bool isExport = false;
    protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
     isExport = true;
      if (e.Item is GridDataItem && isExport)
      {
        GridDataItem item = (GridDataItem)e.Item;
        foreach (TableCell cell in e.Item.Cells)
        {
         cell.Style["font-size"] = "20px"; //changing font size
        }
      }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
       isExport = true;
        foreach (GridDataItem item in RadGrid1.Items)
        {
          LinkButton link = (LinkButton)item.FindControl("LinkButton1");
          item["UniqueName"].Text = link.Text;
       }
        RadGrid1.MasterTableView.ExportToPdf();
    }

    Thanks,
    Shinu.
  7. Lisa
    Lisa avatar
    43 posts
    Member since:
    Jul 2012

    Posted 24 Jul 2012 Link to this post

    Your sample codes uses e.Item, but my code below doesn't support e.Item.

    <asp:ImageButton ID="ImageButton1" ImageUrl="icon1.png" AlternateText="Export to Excel" runat="server" />

            Private Sub exportToExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ImageButton1.Click
                RadGrid1.MasterTableView.ExportToExcel()
            End Sub

  8. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 25 Jul 2012 Link to this post

    Hello Lisa,

    You can set the font size in ItemCreated event. On checking for the bool variable, you can then export the grid in external button click.
    VB:
    Private isExport As Boolean = False
    Protected Sub RadGrid1_ItemCreated(sender As Object, e As Telerik.Web.UI.GridItemEventArgs)
        isExport = True
        If TypeOf e.Item Is GridDataItem AndAlso isExport Then
            Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
            For Each cell As TableCell In e.Item.Cells
                    'changing font size
                cell.Style("font-size") = "20px"
            Next
        End If
    End Sub
    Protected Sub ImageButton1_Click(sender As Object, e As ImageClickEventArgs)
        isExport = True
        For Each item As GridDataItem In RadGrid1.Items
            Dim link As LinkButton = DirectCast(item.FindControl("LinkButton1"), LinkButton)
            item("UniqueName").Text = link.Text
        Next
        RadGrid1.MasterTableView.ExportToPdf()
    End Sub

    Thanks,
    Shinu.
Back to Top