|
| 1 | +--- |
| 2 | +title: How to wrap and center the Grid column header text |
| 3 | +description: How to wrap and center the Grid column header text |
| 4 | +type: how-to |
| 5 | +page_title: How to wrap and center the Grid column header text |
| 6 | +slug: grid-kb-wrap-and-center-column-header-text |
| 7 | +position: |
| 8 | +tags: |
| 9 | +ticketid: 1507250 |
| 10 | +res_type: kb |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>Grid for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | + |
| 24 | +## Description |
| 25 | +Could you please tell me how I can have the GridColumn title text wrap around and be centered? |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +You can use some custom CSS that aligns text in the center and enables text wrapping as per the example below: |
| 30 | + |
| 31 | +````CSHTML |
| 32 | +@* You can also use the Class parameter of the grid to cascade these rules through a selector so it only affects certain grid instances and not all grids on your app *@ |
| 33 | +
|
| 34 | +<style> |
| 35 | + .k-grid .k-grid-header th .k-cell-inner, |
| 36 | + .k-grid .k-grid-header th .k-cell-inner > .k-link { |
| 37 | + display: block; /*defaults to flex which makes aligning text harder in it*/ |
| 38 | + text-align: center; |
| 39 | + white-space: normal; |
| 40 | + } |
| 41 | +
|
| 42 | + .k-column-title { |
| 43 | + white-space: normal; |
| 44 | + } |
| 45 | +</style> |
| 46 | +
|
| 47 | +<TelerikGrid Data="@MyData" Height="400px" |
| 48 | + Pageable="true" Sortable="true" Groupable="true" |
| 49 | + FilterMode="Telerik.Blazor.GridFilterMode.FilterRow" |
| 50 | + Resizable="true" Reorderable="true"> |
| 51 | + <GridColumns> |
| 52 | + <GridColumn Field="@(nameof(SampleData.Id))" Width="120px" /> |
| 53 | + <GridColumn Field="@(nameof(SampleData.Name))" Title="Employee Name" Groupable="false" /> |
| 54 | + <GridColumn Field="@(nameof(SampleData.Team))" Title="Team" /> |
| 55 | + <GridColumn Field="@(nameof(SampleData.HireDate))" Title="Hire Date" /> |
| 56 | + </GridColumns> |
| 57 | +</TelerikGrid> |
| 58 | +
|
| 59 | +@code { |
| 60 | + public IEnumerable<SampleData> MyData = Enumerable.Range(1, 30).Select(x => new SampleData |
| 61 | + { |
| 62 | + Id = x, |
| 63 | + Name = "name " + x, |
| 64 | + Team = "team " + x % 5, |
| 65 | + HireDate = DateTime.Now.AddDays(-x).Date |
| 66 | + }); |
| 67 | +
|
| 68 | + public class SampleData |
| 69 | + { |
| 70 | + public int Id { get; set; } |
| 71 | + public string Name { get; set; } |
| 72 | + public string Team { get; set; } |
| 73 | + public DateTime HireDate { get; set; } |
| 74 | + } |
| 75 | +} |
| 76 | +```` |
| 77 | + |
| 78 | +## Notes |
| 79 | + |
| 80 | +* If you want full control over the header text contents and rendering, you can use the [column header template]({%slug grid-templates-column-header%}). |
| 81 | + |
0 commit comments