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

Radgrid pdf export

4 Answers 112 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Monika Lather
Top achievements
Rank 1
Monika Lather asked on 31 Aug 2011, 01:22 AM
Hi,
I am trying to export the radgrid data to a pdf and excel file.
I cannot seem to format the headers although I can format the data rows.
I am using Need data source method for databinding and
OnItemCommand, OnItemCreated and OnExcelExportCellFormatting methods for formatting while exporting to pdf or excel.

Thanks!

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 31 Aug 2011, 05:38 AM
Hello Monica,

You can customize the header when exporting to pdf in ItemCommand event as shown below.
C#:
protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
  if (e.CommandName == RadGrid.ExportToPdfCommandName)
  {
     foreach (GridHeaderItem item in RadGrid1.MasterTableView.GetItems(GridItemType.Header))
     {
        item.Style["font-size"] = "15pt";
     }
  }
}

Thanks,
Princy.
0
Monika Lather
Top achievements
Rank 1
answered on 31 Aug 2011, 11:44 PM

Hi,
Below is the code I am using to format my pdf.

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.ExportToPdfCommandName)
        {
            foreach (GridHeaderItem item in RadGrid1.MasterTableView.GetItems(GridItemType.Header))
            {
                item.Style["text-align"] = "left";
            }
            isExport = true;
        }
    }
  
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (isExport)
        {
            FormatData(e.Item);
        }
    }
  
    private void FormatData(GridItem item)
    {
        item.Style["color"] = "#000000"
  
        switch (item.ItemType) 
        {
            case GridItemType.Item:
                {
                   item.Style["background-color"] = "#F5F5F5";
                    break;
                }
            case GridItemType.AlternatingItem:
                {
                   item.Style["background-color"] = "#DCDCDC";
                    break;
                }
            case GridItemType.Header:
                {
                   item.Style["background-color"] = "#708090";//This works and the header the required background color
                    break;
                }
        }
    }
  
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        LoadData();
    }

I am unable to format the header to align left. Help is much appreciated.

Thanks!

0
Jayesh Goyani
Top achievements
Rank 2
answered on 01 Sep 2011, 05:33 AM
Hello,

Please check below code snippet.

protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            if (IsExport)
            {
                foreach (GridHeaderItem hitem in RadGrid1.MasterTableView.GetItems(GridItemType.Header))
                {
                    hitem["ColumnName"].Style["text-align"] = "center";
                 }   
            }
       }

Let me know if any concern.

Thanks,
Jayesh Goyani
0
Princy
Top achievements
Rank 2
answered on 01 Sep 2011, 05:48 AM
Hello Monica,

You can try the following code snippet in ItemCreated event to achieve your scenario.
C#:
bool isPdfExport=false;
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
  isPdfExport=true;
  if(e.Item is GridHeaderItem && isPdfExport)
  {
   GridHeaderItem headerItem = (GridHeaderItem)e.Item;
   foreach (TableCell cell in headerItem.Cells)
     {
       cell.Style["text-align"] = "left";
     }
  }
}

Thanks,
Princy.
Tags
Grid
Asked by
Monika Lather
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Monika Lather
Top achievements
Rank 1
Jayesh Goyani
Top achievements
Rank 2
Share this question
or