Is it possible to properly display HTML that was exported from a RadRichTextBox in a GridView cell?
The user is allowed to use many of RadRichTextBox's formatting features when entering data into a RadRichTextBox. This data is saved to the database using the HtmlFormatProvider. I am currently saving the HTML as an HTML fragment with inline formatting. The HTML is later pulled from the database and used to populate the RadRichTextBox. I am able to retain the HTML formatting with no problem.
However, the HTML is not formatted properly when displayed in a GridView. It is simply displayed as text, including all HTML tags. Setting DisableHTMLRendering to false for the appropriate columns does not work.
I noticed that simple HTML displays correctly. For example, the following mark-up displays "text" in blue letters as expected:
"<html><color=0,0,255>text"
The following is a simple example of HTML exported from the RadRichTextBox using the HtmlFormatProvider:
<p style="margin: 0px 0px 0px 0px;text-align: left;text-indent: 0pt;padding: 0px 0px 0px 0px;"><span style="font-family: 'Times New Roman';font-style: normal;font-size: 16px;color: #0000FF;">Text</span></p>
21 Answers, 1 is accepted
By default, you can display text, formatted using a subset of HTML in a RadGridView GridViewTextBoxColumn cell.
To do this, you should change the DisableHTMLRendering property for the column to false
E.g.
this
.radGridView1.Columns[
"Name"
].DisableHTMLRendering =
false
;
You can then display formatted text that follows the HTML-Like formatting article at this link
If you wanted to go further than that, then it would mean creating a custom cell, containing for example a RichTextBox.
You can learn more about creating custom cells here
Hope that helps
Richard
Also, I tried to create a custom cell using a RadRichTextBoxElement but this causes my grid to fail to display. I am sure I am missing something. I cannot seem to find a good example of creating a custom cell containing a RadRichTextBoxElement. Do you have a working example or a link to one?
Thanks in advance.
You could use a WebBrowser to display the HTML in your sample. Here is some sample code that should help get you started.
Form Designer
namespace
RadControlsWinFormsApp1
{
partial
class
RadForm1
{
/// <summary>
/// Required designer variable.
/// </summary>
private
System.ComponentModel.IContainer components =
null
;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected
override
void
Dispose(
bool
disposing)
{
if
(disposing && (components !=
null
))
{
components.Dispose();
}
base
.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private
void
InitializeComponent()
{
this
.radGridView1 =
new
Telerik.WinControls.UI.RadGridView();
((System.ComponentModel.ISupportInitialize)(
this
.radGridView1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(
this
)).BeginInit();
this
.SuspendLayout();
//
// radGridView1
//
this
.radGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
this
.radGridView1.Location =
new
System.Drawing.Point(0, 0);
this
.radGridView1.Name =
"radGridView1"
;
this
.radGridView1.Size =
new
System.Drawing.Size(641, 511);
this
.radGridView1.TabIndex = 0;
this
.radGridView1.Text =
"radGridView1"
;
//
// RadForm1
//
this
.AutoScaleDimensions =
new
System.Drawing.SizeF(6F, 13F);
this
.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this
.ClientSize =
new
System.Drawing.Size(641, 511);
this
.Controls.Add(
this
.radGridView1);
this
.Name =
"RadForm1"
;
//
//
//
this
.RootElement.ApplyShapeToControl =
true
;
this
.Text =
"RadForm1"
;
this
.ThemeName =
"ControlDefault"
;
((System.ComponentModel.ISupportInitialize)(
this
.radGridView1)).EndInit();
((System.ComponentModel.ISupportInitialize)(
this
)).EndInit();
this
.ResumeLayout(
false
);
}
#endregion
private
Telerik.WinControls.UI.RadGridView radGridView1;
}
}
Form 1
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
Telerik.WinControls;
using
Telerik.WinControls.UI;
using
System.Linq;
namespace
RadControlsWinFormsApp1
{
public
partial
class
RadForm1 : Telerik.WinControls.UI.RadForm
{
public
RadForm1()
{
InitializeComponent();
List<User> users =
new
List<User>();
int
k = 0;
while
(k <= 20)
{
bool
hasBeard = (k % 2 == 0);
users.Add(
new
User(k,
"User "
+ k.ToString(), hasBeard,
"<p style=\"margin: 0px 0px 0px 0px;text-align: left;text-indent: 0pt;padding: 0px 0px 0px 0px;\"><span style=\"font-family: 'Times New Roman';font-style: normal;font-size: 16px;color: #0000FF;\">Text</span></p>"
));
k++;
}
this
.radGridView1.AutoGenerateColumns =
false
;
this
.radGridView1.DataSource = users;
this
.radGridView1.ReadOnly =
false
;
GridViewDecimalColumn idColumn =
new
GridViewDecimalColumn(
"Id"
);
GridViewTextBoxColumn nameColumn =
new
GridViewTextBoxColumn(
"Name"
);
GridViewCheckBoxColumn beardColumn =
new
GridViewCheckBoxColumn(
"HasBeard"
);
WebBrowserColumn htmlColumn =
new
WebBrowserColumn(
"Html"
);
this
.radGridView1.Columns.Add(idColumn);
this
.radGridView1.Columns.Add(nameColumn);
this
.radGridView1.Columns.Add(beardColumn);
this
.radGridView1.Columns.Add(htmlColumn);
this
.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
this
.radGridView1.BestFitColumns();
foreach
(GridViewRowInfo row
in
this
.radGridView1.ChildRows)
{
row.Height = 50;
}
this
.radGridView1.AllowRowResize =
false
;
}
}
public
class
User
{
public
User(
int
id,
string
name,
bool
hasBeard,
string
html)
{
Id = id;
Name = name;
HasBeard = hasBeard;
Html = html;
}
public
User()
{ }
public
int
Id
{
get
;
set
;
}
public
string
Name
{
get
;
set
;
}
public
bool
HasBeard
{
get
;
set
;
}
public
string
Html
{
get
;
set
;
}
}
}
WebBrowserCellElement
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
Telerik.WinControls;
using
Telerik.WinControls.UI;
using
Telerik.WinControls.RichTextBox;
using
Telerik.WinControls.RichTextBox.Model.Styles;
namespace
RadControlsWinFormsApp1
{
public
class
WebBrowserCellElement : GridDataCellElement
{
public
WebBrowserCellElement(GridViewColumn column, GridRowElement row)
:
base
(column, row)
{
}
private
RadWebBrowserElement _webBrowserElement;
protected
override
void
CreateChildElements()
{
base
.CreateChildElements();
_webBrowserElement =
new
RadWebBrowserElement();
this
.Children.Add(_webBrowserElement);
}
protected
override
void
SetContentCore(
object
value)
{
if
(
this
.Value !=
null
&&
this
.Value != DBNull.Value)
{
this
._webBrowserElement.DocumentText = value.ToString();
}
}
protected
override
Type ThemeEffectiveType
{
get
{
return
typeof
(GridDataCellElement);
}
}
public
override
bool
IsCompatible(GridViewColumn data,
object
context)
{
return
data
is
WebBrowserColumn && context
is
GridDataRowElement;
}
}
}
WebBrowserColumn
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
Telerik.WinControls;
using
Telerik.WinControls.UI;
namespace
RadControlsWinFormsApp1
{
public
class
WebBrowserColumn : GridViewDataColumn
{
public
WebBrowserColumn(
string
fieldName)
:
base
(fieldName)
{
}
public
override
Type GetCellType(GridViewRowInfo row)
{
if
(row
is
GridViewDataRowInfo)
{
return
typeof
(WebBrowserCellElement);
}
return
base
.GetCellType(row);
}
}
}
Hope that helps
Richard
The html formatting feature available when setting the DisableHTMLRendering property to false supports only a limited number of html tags. Full list of them is available in our online documentation. This feature does not support css styles.
Currently, it is not possible to use our RadRichTextBox as cell content presenter and the only option is the one provided by Richard.
If you have any further questions, we will be glad to help.
Greetings,
Jack
the Telerik team
Also how do I get the grid the expand the row height automatically to display the contents?
FYI: I'm using Winforms.RadGridView 2013 Q2 version
Thank you for writing.
Are you sure you are wrapping your cell content with a <html> tag, because that is what is needed to use the HTML formatting. The following code seems to work fine on my end:
<
html
><
b
><
span
style
=
"color: #006600"
> New </
span
></
b
></
html
>
To make the rows size themselves automatically according to their content, you need to set the AutoSizeRows property of the grid to true:
grid.AutoSizeRows =
true
;
And enable text wrapping on the columns:
grid.Columns.Add(
new
GridViewTextBoxColumn(
"Col"
)
{
DisableHTMLRendering =
false
,
WrapText =
true
,
});
Additionally the AutoSizeRows mode will make the rows collapse if they have no content, that is why you need to set their MinHeight property if needed:
for
(
int
i = 0; i < 10; i++)
{
GridViewRowInfo row = grid.Rows.AddNew();
row.MinHeight = 20;
}
I hope this helps.
Regards,
George
Telerik
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
I followed your instructions to format the text as HTML, but now some of the rendered text appears to be cropped:
I tried changing the font but I still got the same results... do you have any idea what might be causing this?
PS: I'm using telerik version 2012 Q1
Thank you for replying.
Themos, I was not able to reproduce this behavior on my end with the version you specified, or with the latest version. I would like to kindly ask you to provide me with the code you are using to format the RadGridView​ cells and the html which you are setting as a text, this will allow me to more precisely test your case.
Looking forward to your reply.
Regards,
George
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
I was able to fix the problem, I had to comment out this line:
rgvWarnings.Columns["Warning"].WrapText = true;
but also the position of the empty spaces on the stored procedure populating the column seemed to affect it as well...
I am glad that you were able to resolve the matter.
Let me know, should you have further questions.
Regards,
George
Telerik
Hi Richard,
I would like to ask how to do of in ? I would like to see the full text(Note column) in Radgridview and I would like to increase row depend on inside the text. Please see my attached file.
The solution Richard suggested uses a custom cell element which internally hosts the standard Winforms WebBrowser control. It also iterates the rows and sets them with a fixed height. You can try using a separate web browser control and set its DocumentText property with the Html. Then you can get the height of the scroll rectangle as discussed here: https://social.msdn.microsoft.com/Forums/windows/en-US/1a825a40-f93e-478f-98cc-ccdcb82f4189/how-to-get-webbrowser-document-height?forum=winforms. Later you can use this value to set the height of each row.
I hope this helps.
Regards,
Hristo
Progress Telerik
Is it possible to display html with table tags within a grid cell.
I tried implementing the solution posted by Richard (using WebBrowser). But I'm facing below issues:
- Not able to edit the cell value
- While scrolling down, the cell content is overlapping the header cell.
Please see the attached
We have a feature request for providing means of displaying and editing Rich Text Content in RadGridView. The item is logged on our feedback portal, here: ADD. RadRichTextEditor - allow RadRichTextBoxEditorElement to be added as custom editor in RadGridView, as elements support clipping. You can subscribe to it and be updated when its status changes.
I am also attaching a project in C# with a custom implementation of the RichTextColumn. It can work with the HTML format and uses a hosted RadRichTextEditor control. I hope that this solution would fit your local setup, however, please note that it may not handle all possible cases, e.g. the custom rich-text cells will not be clipped while scrolling the grid.
I hope this will help.
Regards,
Hristo
Progress Telerik
Hi
I am using the solution with GridViewRichTextColumn and it works great in a RadGridView.
I am trying to use it in GridViewRowDetailsExtended (provided by Richard Slade in https://www.telerik.com/forums/details-view-under-row-in-gridview). If you look at the attached image, at the right side the cell goes over the scroll bar. Is there a property or something that I am missing?
Also I tried with child GridViewTemplate, but it not expands.
Paulo
Hello, Paulo,
Following the provided information it seems that you have built a complex custom layout in RadGridView using custom row details and .custom column with RichTextEditor. Considering the picture, it seems like the custom cell that hosts the RichTextEditorElement overlaps the grid and hides the vertical scrollbar that is shown at the right. You should consider setting an appropriate size/width for that cell in order to prevent it from overlapping the scrollbar.
However, I am not familiar with the exact setup that you have in RadGridView. If you need further assistance it would be of great help if you can provide a code snippet with the customized grid that you use. Thus, we could be able to investigate precisely the custom case and provide a suitable solution.
Looking forward to your reply.
Regards,
Nadya
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.