I have a grid with several databound columns. Some of the columns contain multiline data. The data is captured and stored in the database correctly (with carriage returns), but in the grid itself the carriage returns are removed and the lines all just run together into one long line in the cell instead of displaying 3 lines in the cell.
Does a databound grid column have any setting for multiline display within a cell?
Thanks
Does a databound grid column have any setting for multiline display within a cell?
Thanks
3 Answers, 1 is accepted
0

Shinu
Top achievements
Rank 2
answered on 08 Jan 2014, 05:50 AM
Hi Lisa,
Browsers normally ignore white spaces in the HTML source. You should either modify the strings before binding the RadGrid control, or modify them in the RadGrid ItemDataBound event, or set DataFormatString="<pre>{0}</pre>" for the respective column. You may need to add some font styles for the <pre> element so that it looks like the rest of the RadGrid content.
By default, text inside a <pre> tag is wrapped only where there is a new line. If the text has new lines at too few places, you will have to use ItemDataBound to insert <br /> tags at the places where you want new lines (paragraphs) to appear in the browser.
Below is a sample code that displays use of <pre> tag and giving space through ItemDataBound:
ASPX:
C#:
Thanks,
Shinu
Browsers normally ignore white spaces in the HTML source. You should either modify the strings before binding the RadGrid control, or modify them in the RadGrid ItemDataBound event, or set DataFormatString="<pre>{0}</pre>" for the respective column. You may need to add some font styles for the <pre> element so that it looks like the rest of the RadGrid content.
By default, text inside a <pre> tag is wrapped only where there is a new line. If the text has new lines at too few places, you will have to use ItemDataBound to insert <br /> tags at the places where you want new lines (paragraphs) to appear in the browser.
Below is a sample code that displays use of <pre> tag and giving space through ItemDataBound:
ASPX:
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
Width
=
"200px"
AutoGenerateColumns
=
"false"
OnNeedDataSource
=
"RadGrid_NeedDataSource"
>
<
MasterTableView
>
<
Columns
>
<
telerik:GridBoundColumn
DataField
=
"Column1"
HeaderText
=
"Column"
DataFormatString="<pre>{0}</
pre
>" />
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
<
br
/>
<
br
/>
<
telerik:RadGrid
ID
=
"RadGrid2"
runat
=
"server"
Width
=
"200px"
AutoGenerateColumns
=
"false"
OnItemDataBound
=
"RadGrid_ItemDataBound"
OnNeedDataSource
=
"RadGrid_NeedDataSource"
>
<
MasterTableView
>
<
Columns
>
<
telerik:GridBoundColumn
DataField
=
"Column1"
HeaderText
=
"Column"
/>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
C#:
protected
void
RadGrid_NeedDataSource(
object
sender, GridNeedDataSourceEventArgs e)
{
DataTable dt =
new
DataTable();
DataRow dr;
int
colsNum = 1;
int
rowsNum = 2;
string
colName =
"Column"
;
for
(
int
j = 1; j <= colsNum; j++)
{
dt.Columns.Add(String.Format(
"{0}{1}"
, colName, j));
}
for
(
int
i = 1; i <= rowsNum; i++)
{
dr = dt.NewRow();
for
(
int
k = 1; k <= colsNum; k++)
{
dr[String.Format(
"{0}{1}"
, colName, k)] = String.Format(@
"{0}{1} {0}{1} {0}{1} {0}{1} Row{2} Row{2} Row{2} Row{2}"
, colName, k, i);
}
dt.Rows.Add(dr);
}
(sender
as
RadGrid).DataSource = dt;
}
protected
void
RadGrid_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem && !e.Item.IsInEditMode)
{
TableCell cell = (e.Item
as
GridDataItem)[
"Column1"
];
cell.Text = cell.Text.Replace(
"\n"
,
"<br />"
);
}
}
Thanks,
Shinu
0

Lisa
Top achievements
Rank 1
answered on 10 Jan 2014, 01:51 PM
Thanks Shinu. You pointed me in the right direction.
I ended up modifying the SQL data source view to REPLACE carriage returns with ' \n ' in the "nonconformance" field.
I then added code to the grid's ItemDataBound event as follows (VB.NET):
This worked fine.
BTW I tried your first suggestion of using the <pre> tag which was good to know but did not work well in my case. The field usually has several lines of data. When using <pre>, it did start each line on a new line but the individual lines did not wrap as the user wanted.
I ended up modifying the SQL data source view to REPLACE carriage returns with ' \n ' in the "nonconformance" field.
I then added code to the grid's ItemDataBound event as follows (VB.NET):
Private Sub RadGrid1_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
If (TypeOf (e.Item) Is GridDataItem) Then
Dim dataBoundItem As GridDataItem = e.Item
Dim cell As TableCell = dataBoundItem("Nonconformance")
cell.Text = cell.Text.Replace(" \n ", "<br />")
End If
End Sub
This worked fine.
BTW I tried your first suggestion of using the <pre> tag which was good to know but did not work well in my case. The field usually has several lines of data. When using <pre>, it did start each line on a new line but the individual lines did not wrap as the user wanted.
0

Mark
Top achievements
Rank 1
answered on 10 Oct 2014, 06:29 PM
I know this is a really late reply but I had the same problem and used DetailItemTemplate and it worked perfectly