Skip to content

Commit d7a3fe3

Browse files
kb(Grid): line breaks in grid column (#2096)
* kb(Grid): line breaks in grid column * chore(Grid): fixes as per comments
1 parent cba0c4e commit d7a3fe3

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

knowledge-base/grid-line-breaks.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
title: Render Line Breaks in Grid Column
3+
description: How to create new lines inside a Grid column.
4+
type: how-to
5+
page_title: Render Line Breaks in Grid Column
6+
slug: grid-kb-line-breaks
7+
position:
8+
tags:
9+
ticketid: 1650530
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+
## Description
24+
25+
This knowledge base article answers the following questions:
26+
27+
* How do I display line breaks inside a Grid column?
28+
* Is it possible to render HTML inside a Grid column?
29+
30+
## Solution
31+
32+
1. To create a new line break in the HTML content of a Grid cell, replace the `\n` segment with the `<br>` HTML tag.
33+
2. To render the `<br>` HTML tag from a string, use a <a href="https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.markupstring?view=aspnetcore-8.0" target="_blank"><code>MarkupString<code></a>.
34+
3. To define the `MarkupString`, use the column's `Template`.
35+
36+
````CSHTML
37+
<TelerikGrid Data="@GridData" Height="400px">
38+
<GridColumns>
39+
<GridColumn Field="@(nameof(Item.Text))" Title="Test">
40+
<Template>
41+
@{
42+
var item = (Item)context;
43+
44+
@(new MarkupString($"{item.Text.Replace("\n", "<br>")}"))
45+
}
46+
</Template>
47+
</GridColumn>
48+
<GridColumn Field="@(nameof(Item.Name))" />
49+
</GridColumns>
50+
</TelerikGrid>
51+
52+
@code {
53+
private IEnumerable<Item> GridData = Enumerable.Range(1, 10).Select(x => new Item
54+
{
55+
Id = x,
56+
Name = "Item " + x,
57+
Text = "Test\nTest\nTest"
58+
});
59+
60+
public class Item
61+
{
62+
public int Id { get; set; }
63+
public string Name { get; set; }
64+
public string Text { get; set; }
65+
}
66+
}
67+
````
68+
69+
## See Also
70+
71+
* [Grid Column Template]({%slug grid-templates-column%})

0 commit comments

Comments
 (0)