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.Visible = True For Each item As GridPagerItem In RadGridGenerateList.MasterTableView.GetItems(GridItemType.Pager) 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
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.
| 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 |
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 SelectEnd SubIn 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 IfEnd SubRegards
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.
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 Object, ByVal 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.
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.
| 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 |