I have converted the vb code at the following post (oct 1)to c#, see attachment.
http://stackoverflow.com/questions/32866795/formatted-text-inside-cell-on-datagridview
It renders a rich text into a winforms DataGridView cell.
Could someone please in converting it work with a Telerik WInform Gridview?
10 Answers, 1 is accepted
Thank you for writing.
Please, note that our controls support HTML-like formatting and you can just use the CellFormatting event to format any particular parts of the text:
private
void
RadGridView1_ViewCellFormatting(
object
sender, CellFormattingEventArgs e)
{
if
(e.CellElement.ColumnIndex == 1 && e.CellElement.Text.Length > 3)
{
e.CellElement.DisableHTMLRendering =
false
;
e.CellElement.Text =
string
.Format(
"<html><span style=\"color: red\">{0}</span></html>{1}"
, e.CellElement.Value.ToString().Substring(0, 3), e.CellElement.Value.ToString().Substring(3));
}
}
Another approach would be to manually paint the text in the cells. More information is available here: Painting and drawing in cells.
I hope this helps. Should you have any other questions do not hesitate to ask.
Dimitar
Telerik
Hi Dimitar,
thank you for your answer. Unfortunately HTML formatting is no solution for me.
Attached is a converted sample code to work with Telerik Winforms (zip renamed to .png). It works like this:
The content is taken from first the richtextbox outside the gridview. Adding / removing its text will cause all richtextboxes rendered in all grid cells to change, including rtb's vertical scrollbar. You have to move the mouse over the grid cell to make it refresh.
In an ImageColumn the winforms richtextbox is rendered without its WinForms scrollbars by CellPaint.
In order to fake the winforms richtextbox's vertical scrollbal, a Telerik radVScrollbar is rendered by CellPaint in the same cell.
When the active cell is selected, and the row size is reduced so much that the scroll thumb cannot fully render any more, so the thumb is hidden programmatically.
But for non active cells it will not work sometimes: upon row-height-change the vertical scroll thumb will not be hidden, but it will mess up the scroll arrow sometimes. This is probably due to the global vScrollbar and the sequence of the used events radGridView1_RowHeightChanged and _CellPaint and the wrong setting of the vScrollBar's size.
Anyhow I want to load the richtextboxes with unique content in each cell from a generic list of objects containing rtf string, thus different vertical scrollbar sizes are needed.
Also when resizing the column with the richtextbox, the width will be reduced, but after a while the vertical RadScrollbar starts to disappear.
I would appreciate changed code from you with fixes, I am sure there is a more clear approach.
Thank you for writing back.
The following example shows how you can create a custom column where each cell contains a RadRichTextEditor. All you need to do to use it is to add the custom columns and set the cells values to an string:
class
RTBCellElement : GridDataCellElement
{
public
RTBCellElement(GridViewColumn column, GridRowElement row)
:
base
(column, row)
{
this
.DrawText =
false
;
}
RadRichTextEditor editor =
new
RadRichTextEditor();
RtfFormatProvider provider =
new
RtfFormatProvider();
protected
override
void
CreateChildElements()
{
base
.CreateChildElements();
RadHostItem host =
new
RadHostItem(editor);
host.ShouldHandleMouseInput = !
false
;
host.NotifyParentOnMouseInput = !
false
;
host.HostedControl.MouseDown += HostedControl_MouseDown;
this
.Children.Add(host);
}
private
void
HostedControl_MouseDown(
object
sender, MouseEventArgs e)
{
editor.Capture =
true
;
}
protected
override
void
SetContentCore(
object
value)
{
if
(value !=
null
&& value != DBNull.Value)
{
editor.Document = provider.Import(value.ToString());
}
}
protected
override
Type ThemeEffectiveType
{
get
{
return
typeof
(GridDataCellElement);
}
}
public
override
bool
IsCompatible(GridViewColumn data,
object
context)
{
return
data
is
RTBColumn && context
is
GridDataRowElement;
}
}
public
class
RTBColumn : GridViewDataColumn
{
public
RTBColumn()
:
base
()
{
}
public
override
Type GetCellType(GridViewRowInfo row)
{
if
(row
is
GridViewDataRowInfo)
{
return
typeof
(RTBCellElement);
}
return
base
.GetCellType(row);
}
}
I hope this helps.
Regards,
Dimitar
Telerik
Hi Dimitar,
thank you for writing back. I have tried the above code. Findings:
1. Performance is poor. I need minimum 2 x 300 richtextboxes in two columns. Scrolling is sluggish, unbound loading the richtextboxes takes way too much time.
2. Visual glitch in the grid header: during scrolling the richtextbox will overwrite the gridview's header
To solve #1: the winforms richtextbox performance is just ok, I want to use that.
Ho would you solve #2?
Thank you for writing back.
1. Please note that RadRichTextEditor provides rich functionality and has many features. It is expected that creating many controls to affect the performance. Since RadgridView is using UI Virtualization you should consider reducing the count of editors which are visible at the same time.
2. Please note that in this case you are adding a control which is drawn over the element. The only possible way to solve this is to create a custom header cell and add another control to it (for example RadLabel).
I hope this will be useful.
Regards,
Dimitar
Telerik
Hello Dimitar,
thank you. Now I am sure, that my approach is "Painting and drawing in cells" which you indicated in your earlier post.
I have worked out a prototype solution using this approach. I have also added a vertical RadScrollBarElement to the cell as a child element.
This will reflect reflect changes neither from its arrow-click events nor from click between the thumb and the arrow. However thumb grab actions will be reflected. I have issued a ticket, and uploaded my solution. I hope with the help of support I can get progress on this. I start to feel frustrated after having tried so many directions without success.
Best Regards
Laszlo
Thank you for writing back.
There are limitations of the approach with the rich text editor. Simply it is not designed for this purpose. Please, note that this is not a common case and usually the HTML formatting is sufficient for displaying formatted text.
One more possibility for this case is to render the text in a rich text box, save it as an image and show it in the grid:
void
button_Click(
object
sender, EventArgs e)
{
DocumentWebLayoutPresenter presenter = radRichTextEditor1.RichTextBoxElement.ActiveEditorPresenter
as
DocumentWebLayoutPresenter;
Bitmap bmp =
new
Bitmap(Convert.ToInt32( presenter.ScrollableWidth), Convert.ToInt32( presenter.ScrollableHeight));
this
.radRichTextEditor1.DrawToBitmap(bmp,
new
System.Drawing.Rectangle(
new
System.Drawing.Point(0, 0),
new
System.Drawing.Size(Convert.ToInt32(presenter.ScrollableWidth), Convert.ToInt32(presenter.ScrollableHeight))));
bmp.Save(@
"C:\test.png"
);
}
I hope this will be useful.
Regards,
Dimitar
Telerik
Hi Dimitar,
thank you very much for your efforts.
I have issued a support ticket and received support for the scroll topic. A solution seems to emerge out of this. I have still some more work to do on it, but the outline is visible..
Best Regards
Laszlo
I am glad that you have found a proper solution for this case. Do not hesitate to contact us if you have other questions.
Dimitar
Telerik