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

[Solved] Export to pdf - column alignment

8 Answers 502 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Clive Hoggar
Top achievements
Rank 1
Clive Hoggar asked on 03 Oct 2009, 05:21 PM
Hi

I have looked at several threads on this but have not yet found a solution that works.
I must be missing something.

Using this code behind to set the column width works OK so I was trying to add the
column alignment in the same sub, but the alignment always defaults to left for items and centre for headers.
What am I missing?

 

Protected Sub ButtonGenPdf_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonGenPdf.Click

        RadGridGenerateList.MasterTableView.Font.Name = "Arial"
        RadGridGenerateList.ExportSettings.ExportOnlyData = False
        RadGridGenerateList.ExportSettings.OpenInNewWindow = True
        RadGridGenerateList.ExportSettings.IgnorePaging = True
        RadGridGenerateList.ExportSettings.Pdf.PageHeight = Unit.Parse("210mm")
        RadGridGenerateList.ExportSettings.Pdf.PageWidth = Unit.Parse("296mm")

        RadGridGenerateList.Visible = True
        RadGridGenerateList.Rebind()

        For Each item As GridPagerItem In RadGridGenerateList.MasterTableView.GetItems(GridItemType.Pager)
            item.PagerContentCell.Controls.Clear()
        Next
        DefineColumnWidths()

        RadGridGenerateList.MasterTableView.ExportToPdf()

    End Sub

Protected Sub DefineColumnWidths()  
        For Each col As GridColumn In RadGridGenerateList.MasterTableView.RenderColumns  
            Select Case col.UniqueName  
                Case "Region"  
                    col.HeaderStyle.Width = Unit.Pixel(100)  
                    col.HeaderStyle.HorizontalAlign = HorizontalAlign.Left  
                    Exit Select  
                Case "Name"  
                    col.HeaderStyle.Width = Unit.Pixel(200)  
                    col.HeaderStyle.HorizontalAlign = HorizontalAlign.Left  
                    Exit Select  
 
 etc etc

Thanks a lot for suggestions!

Clive

8 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 05 Oct 2009, 01:48 PM
Hello Clive,

For your convenience I created a sample project that demonstrate how to align the text in the grid items. Please notice that when the item type is GridHeaderItem I set the alignment to every cell (TH element) and not to the whole row.

Kind regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Clive Hoggar
Top achievements
Rank 1
answered on 05 Oct 2009, 04:09 PM
Hi Daniel

Thanks for this.   I have been trying to adapt it to the case I need, which is that the 
column alignment really needs to be case by case.  

In general text columns should be left-aligned and numbers should be right-aligned.
I have tried a lot of different ways to adapt this sub but could not get to an error-free result.
Please advise!

 Protected Sub FormatItem(ByRef item As GridItem) 
 
        Select Case item.GetType().Name 
            Case "GridHeaderItem" 
                For Each cell As TableCell In item.Cells 
                    cell.Style("text-align") = "left" 
                Next 
                Exit Select 
            Case "GridDataItem" 
                item.Style("text-align") = "right" 
                Exit Select 
        End Select 
    End Sub 

0
Daniel
Telerik team
answered on 08 Oct 2009, 02:50 PM
Hello Clive,

You can use TryParse to distinguish whether the cell contains a value:
Protected Sub FormatItem(ByRef item As GridItem)
    Select Case item.GetType().Name
        Case "GridHeaderItem"
            For Each cell As TableCell In item.Cells
                cell.Style("text-align") = "center"
            Next
            Exit Select
 
        Case "GridDataItem"
            For Each cell As TableCell In item.Cells
                Dim value As Double
                If Double.TryParse(cell.Text, value) Then
                    cell.Style("text-align") = "right"
                End If
            Next
    End Select
End Sub

In this case you should use ItemDataBound instead of ItemCreated
Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
    If IsExport Then
        FormatItem(e.Item)
    End If
End Sub

Regards
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Clive Hoggar
Top achievements
Rank 1
answered on 08 Oct 2009, 04:34 PM
Hi Daniel

Thanks.  I have tried the code now, but I am getting an intellisense and a runtime error
"Name 'IsExport' not declared".  What am I missing?

Thanks

Clive
0
Princy
Top achievements
Rank 2
answered on 09 Oct 2009, 06:30 AM
Hi Clive,

You have to actually globally declare the IsExport variable somewhere in the code behind page. Here's how you have to be declaring it:
vb:
Public IsPdfExport As Boolean = False 
Protected Sub RadGrid1_ItemCommand(ByVal source As ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand 
   If e.CommandName = RadGrid.ExportToPdfCommandName Then 
     IsPdfExport = True 
     RadGrid1.ExportSettings.IgnorePaging = True 
   End If 
End Sub 

Thanks
Princy.
0
Clive Hoggar
Top achievements
Rank 1
answered on 09 Oct 2009, 09:44 AM
Hi Princy

Thanks this does work as specified .. but the pdf layout looks a mess, because in fact some of the fields that actually contain prices are defined as text fields (for legacy reasons), so the wrong treatment gets applied.

Bottom line, I do need to specify the headings and the item alignment on a column-by-column basis.
How can I do that?

Thanks a lot

Clive

0
Daniel
Telerik team
answered on 14 Oct 2009, 12:18 PM
Hello Clive,

1) You can wrap the cell contents in   tags and it won't pass the TryParse check. The easiest way is to use DataFormatString property.

2) Please try the following snippet:
Protected Sub FormatItem(ByRef item As GridItem)
    Select Case item.GetType().Name
        Case "GridHeaderItem"
            For Each column As GridColumn In RadGrid1.Columns
                Dim colName As String = column.UniqueName
                Dim headerItem As GridHeaderItem = CType(item, GridHeaderItem)
                Select Case colName
                    Case "ID"
                        headerItem(colName).Style("text-align") = "center"
                        Exit Select
                    Case "Name"
                        headerItem(colName).Style("text-align") = "right"
                End Select
            Next
            Exit Select
            ....

Regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Clive Hoggar
Top achievements
Rank 1
answered on 14 Oct 2009, 02:40 PM
Thanks Daniel.
I couldn't get the items to be consistently recognized as number format or not so I used the one-by-one approach as for the header items:
Case "GridDataItem" 
                For Each column As GridColumn In RadGridGenerateList.Columns 
                    Dim colName As String = column.UniqueName 
                    Dim ItemCell As GridDataItem = CType(item, GridDataItem) 
                    Select Case colName 
                        Case "PricePerBottle" 
                            ItemCell(colName).Style("text-align") = "right" 
                            Exit Select 
                        Case "PricePerBottleEuro" 
                            ItemCell(colName).Style("text-align") = "right" 
                            Exit Select 
etc etc 


regards
Clive
Tags
Grid
Asked by
Clive Hoggar
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Clive Hoggar
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or