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

RadGrid Export to PDF - how to dynamically set column widths

4 Answers 342 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Randel
Top achievements
Rank 1
Randel asked on 16 Oct 2014, 08:42 PM
Hello,

I'm looking for help on exporting data from a RadGrid to a PDF file, via the ExportToPDF control button.  I've read a handful of threads on this, but none are working.  I'm trying to get the cell alignment to be correct for text (left-aligned) and numeric values (right-aligned).  Part of my difficulty is the fact the visible columns is dynamic, so I don't have a unique name to look for.  This also causes a possible issue with too many columns to fit onto the page without manually formatting the output.

Because I cannot effect the client's experience (the UI layout), I've moved my code from the 'ItemCommand' event to the 'ItemCreated' event.  This allows me to get all the column to fit on the page, but I'm still having issues with setting the text cell alignment left and the numeric alignment right.  I'm able to set them all to either: left, right, center, none.  Another issue I have, is that by doing this in the ItemCreated event, there's no data to determine what type of text is going to be in the cell.
protected void RadGrid_AssetList_ItemCreated( object sender, GridItemEventArgs e )
{
 if(IsExportButtonClicked){
  FormatPdfOutput(RadGrid_AssetList);
 
  var item = e.Item as GridDataItem;
 
  if(item != null){
   var dataItem = item;
 
   foreach(TableCell cell in dataItem.Cells){
    cell.Style["text-align"] = "center";
   }
  }
  else{
   var gridHeaderItem = e.Item as GridHeaderItem;
 
   if(gridHeaderItem != null){
    var headerItem = gridHeaderItem;
 
    foreach(TableCell cell in headerItem.Cells){
     cell.Style["text-align"] = "center";
    }
   }
   else{
    var gridFooterItem = e.Item as GridFooterItem;
 
    if(gridFooterItem != null){
     var footerItem = gridFooterItem;
 
     foreach(TableCell cell in footerItem.Cells){
      cell.Style["text-align"] = "center";
     }
    }
   }
  }
 }
}


private void FormatPdfOutput(RadGrid radGrid){
 radGrid.ExportSettings.IgnorePaging = true;
 radGrid.ExportSettings.ExportOnlyData = true;
 radGrid.ExportSettings.HideStructureColumns = true;
 
 double marginWidth = radGrid.ExportSettings.Pdf.PageLeftMargin.Value
               + radGrid.ExportSettings.Pdf.PageRightMargin.Value;
 
 double printArea = radGrid.ExportSettings.Pdf.PageWidth.Value - marginWidth;
 
 //if the print area of the page is smaller than the RadGrid width (total column width), change to
 //percent based widths and evenly break up the columns.
 if(printArea < radGrid.Width.Value){
  //count the number of columns that are both visible and have header text.
  var visibleColumnCount = radGrid.Columns.Cast<GridColumn>().Count(column => column.Visible);
 
  //calculate the percentage to evenly display all the columns
  var tempWidth = 1.0 / (double)visibleColumnCount;
 
  foreach(GridColumn column in radGrid.Columns){
   if(column.Visible){
    column.Visible = column.HeaderText.Length > 0;
   }
 
   column.HeaderStyle.Width = column.Visible
                      ? Unit.Percentage(tempWidth)
                      : Unit.Percentage(0);
 
   column.HeaderStyle.Wrap = true;
 
  if(column.Visible){
   column.HeaderStyle.HorizontalAlign = column as GridNumericColumn == null
                              ? HorizontalAlign.Left
                              : HorizontalAlign.Right;           
   }
  }
 }
}

4 Answers, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 21 Oct 2014, 11:10 AM
Hello Randel,

Basically you can get the column DataType and check whether a column is a numeric or text type. For your convenience I prepared a small sample and attached it to this thread.

Regards,
Kostadin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Randel
Top achievements
Rank 1
answered on 21 Oct 2014, 12:38 PM
Thanks for the help Kostadin,

Fortunately, I figured that right before I got your reply came through.  However, I'm doing something just a bit different with my comparison and would like your thoughts on it.  I'm actually doing a dual comparison, only because I wanted my 'char' columns to be left aligned, on 'DataType.IsValueType' and 'DataType' != typeof(char).  Do you see any problem with that?

Also, if the 'ItemCreated' event can be used to adjust this formatting, why when I get the GridColumn, from the RadGrid.Columns collection, setting 'HorizontalAlgin' to either: 'HorizontalAlign.Left' or 'HorizontalAlign.Right' doesn't seem to take effect.  You can see that I have that specific code commented out in my 'FormatPdfOutput' method.

NOTE: the 'IsExportButtonClicked' is a property that returns true if either ('isExcelExport' or 'isPdfExport') local variable are true.  Each are set true, based on a switch statement comparing the command type ('ExportToExcelCommandName' and 'ExportToPdfCommandName'), within the 'ItemCommand' event.
//-----------------------------------------------------------------------------
protected void RadGrid_AssetList_ItemCreated( object sender, GridItemEventArgs e )
{
  if ( IsExportButtonClicked )
  {
    FormatPdfOutput(RadGrid_AssetList);
 
    var item = e.Item as GridDataItem;
 
    if (item != null)
    {
      AlignGridTableCells(item);
    }
    else
    {
      var gridHeaderItem = e.Item as GridHeaderItem;
 
      if (gridHeaderItem != null)
      {
        AlignGridTableCells(gridHeaderItem, "center");
      }
      else
      {
        var gridFooterItem = e.Item as GridFooterItem;
 
        if (gridFooterItem != null)
        {
          AlignGridTableCells(gridFooterItem);
        }
      }
    }
  }
}
 
//-----------------------------------------------------------------------------
private void FormatPdfOutput(RadGrid radGrid)
{
  radGrid.ExportSettings.IgnorePaging = true;
  radGrid.ExportSettings.ExportOnlyData = true;
  radGrid.ExportSettings.HideStructureColumns = true;
 
  double marginWidth = radGrid.ExportSettings.Pdf.PageLeftMargin.Value
                + radGrid.ExportSettings.Pdf.PageRightMargin.Value;
 
  double printArea = radGrid.ExportSettings.Pdf.PageWidth.Value - marginWidth;
 
  //if the print area of the page is smaller than the RadGrid width (total column width),
  //change to percent based widths and evenly break up the columns.
  if (printArea < radGrid.Width.Value)
  {
    var visibleColumnCount = radGrid.Columns.Cast<GridColumn>().Count(column => column.Visible);
 
    //calculate the percentage to evenly display all the columns
    var tempWidth = 1.0 / (double)visibleColumnCount;
 
    foreach (GridColumn column in radGrid.Columns)
    {
      //this is here to ensure the command columns aren't visible
      if (column.Visible)
      {
        column.Visible = column.HeaderText.Length > 0;
      }
 
      column.HeaderStyle.Width = column.Visible
                         ? Unit.Percentage(tempWidth)
                         : Unit.Percentage(0);
 
      column.HeaderStyle.Wrap = true;
 
      //if ( column.Visible )
      //{
      //  column.HeaderStyle.HorizontalAlign = column as GridNumericColumn == null
      //                                     ? HorizontalAlign.Left
      //                                     : HorizontalAlign.Right;           
      //}
    }
  }
}
 
//-----------------------------------------------------------------------------
private void AlignGridTableCells(GridItem gridItem)
{
  if ( gridItem != null )
  {
    //dynamically set the cell's alignment
    for (int i = 0; i < gridItem.Cells.Count; i++)
    {
      var cell = gridItem.Cells[i] as GridTableCell;
 
      if (cell != null)
      {
        //for any value type, but 'char', align-right; otherwise, align-left
        if (cell.Column.DataType.IsValueType && cell.Column.DataType != typeof(char))
        {
          gridItem.Cells[i].Style["text-align"] = "right";
        }
        else
        {
          gridItem.Cells[i].Style["text-align"] = "left";
        }
      }
    }       
  }
}
 
//-----------------------------------------------------------------------------
private void AlignGridTableCells(GridItem gridItem, string alignment)
{
  if ( gridItem != null  )
  {
    //dynamically set the cell's alignment
    for (int i = 0; i < gridItem.Cells.Count; i++)
    {
      var cell = gridItem.Cells[i] as GridTableCell;
 
      if (cell != null)
      {
        gridItem.Cells[i].Style["text-align"] = alignment;
      }
    }
  }
}
0
Kostadin
Telerik team
answered on 24 Oct 2014, 07:32 AM
Hi Randel,

Both approaches mine and yours are pretty much identical so you can use either one of them. Regards the issue with applying a HorizontalAlign property, note that this property set an alignment of the row (<tr> element) and not on the cell (<td> element). In such cases the PDF export parser which we are using could not apply the text alignment properly of the cell.

Regards,
Kostadin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Randel
Top achievements
Rank 1
answered on 24 Oct 2014, 12:16 PM
Kostadin,

Thanks for the confirmation and clarification.

Randel
Tags
Grid
Asked by
Randel
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
Randel
Top achievements
Rank 1
Share this question
or