|
| 1 | +--- |
| 2 | +title: Right Align Grid Header Cells |
| 3 | +description: How to align the text in Grid header and data cells to the right |
| 4 | +type: how-to |
| 5 | +page_title: Right Align Grid Header Cells |
| 6 | +slug: grid-kb-right-align-header-cells |
| 7 | +position: |
| 8 | +tags: grid, header, headers, right, align, text-align |
| 9 | +ticketid: |
| 10 | +res_type: kb |
| 11 | +--- |
| 12 | + |
| 13 | +## Description |
| 14 | + |
| 15 | +I have a Grid that displays numeric data. The numbers in the data cells are aligned to the right with the `TextAlign` column attribute. How do I align the text content of Grid header cells to the right? |
| 16 | + |
| 17 | +## Solution |
| 18 | + |
| 19 | +To align the Grid header cell labels to the right, use custom CSS as per the example below. Note that different CSS rules are needed, depending on the Grid configuration. The CSS code affects all columns. |
| 20 | + |
| 21 | +>caption Align Grid header cells and data cells to the right |
| 22 | +
|
| 23 | +````CSHTML |
| 24 | +<TelerikGrid Data="@GridData" |
| 25 | + Sortable="true" |
| 26 | + FilterMode="GridFilterMode.FilterMenu" |
| 27 | + Class="right-align"> |
| 28 | + <GridColumns> |
| 29 | + <GridColumn Field=@nameof(Product.Name) Title="Product Name" TextAlign="ColumnTextAlign.Right" /> |
| 30 | + <GridColumn Field=@nameof(Product.Price) Title="Price" TextAlign="ColumnTextAlign.Right" DisplayFormat="{0:n2}" /> |
| 31 | + <GridColumn Field=@nameof(Product.Quantity) Title="Units In Stock" TextAlign="ColumnTextAlign.Right" /> |
| 32 | + </GridColumns> |
| 33 | +</TelerikGrid> |
| 34 | +
|
| 35 | +<style> |
| 36 | +
|
| 37 | + .right-align .k-grid-header .k-header { |
| 38 | + /* no sorting, no filtering */ |
| 39 | + text-align: right; |
| 40 | +
|
| 41 | + /* filtering or column menu, no sorting */ |
| 42 | + /*padding-right: calc(1.6em + 14px);*/ |
| 43 | + } |
| 44 | +
|
| 45 | + /* sorting - move the sorting arrow to the left */ |
| 46 | + .right-align .k-cell-inner > .k-link { |
| 47 | + flex-flow: row-reverse nowrap; |
| 48 | + } |
| 49 | +
|
| 50 | + /* filtering or column menu - keep the icon on the RIGHT */ |
| 51 | + .right-align .k-cell-inner .k-column-title { |
| 52 | + padding-right: calc(1.6em + 10px); |
| 53 | + } |
| 54 | +
|
| 55 | + /* OR */ |
| 56 | +
|
| 57 | + /* filtering or column menu - move the icon to the LEFT */ |
| 58 | + /*.right-align .k-grid-header .k-grid-filter, |
| 59 | + .right-align .k-grid-header .k-header-column-menu { |
| 60 | + right: auto; |
| 61 | + left: 0; |
| 62 | + }*/ |
| 63 | +</style> |
| 64 | +
|
| 65 | +@code { |
| 66 | + public List<Product> GridData { get; set; } |
| 67 | +
|
| 68 | + protected override void OnInitialized() |
| 69 | + { |
| 70 | + GridData = new List<Product>(); |
| 71 | + var rnd = new Random(); |
| 72 | +
|
| 73 | + for (int i = 1; i <= 5; i++) |
| 74 | + { |
| 75 | +
|
| 76 | + GridData.Add(new Product() |
| 77 | + { |
| 78 | + ID = i, |
| 79 | + Name = "Product " + i.ToString(), |
| 80 | + Price = (decimal)rnd.Next(1, 100), |
| 81 | + Quantity = (short)rnd.Next(1, 100) |
| 82 | + }); |
| 83 | + } |
| 84 | + } |
| 85 | +
|
| 86 | + public class Product |
| 87 | + { |
| 88 | + public int ID { get; set; } |
| 89 | + public string Name { get; set; } |
| 90 | + public decimal Price { get; set; } |
| 91 | + public short Quantity { get; set; } |
| 92 | + } |
| 93 | +} |
| 94 | +```` |
0 commit comments