【问题标题】:Show HTML in DataGridView control cell in C# desktop app在 C# 桌面应用程序的 DataGridView 控件单元格中显示 HTML
【发布时间】:2018-10-22 15:06:18
【问题描述】:

我有一个 C# 桌面应用程序,它连接到数据库并从数据库中填充 DataGridView(使用数据绑定)。 一列的值是 HTML 格式的值,应在 DataGridView 中显示为 HTML。不幸的是,目前所有内容(例如标签)都是以原始格式(即非 HTML 格式)编写的。

我已经在互联网上找到了一个site,上面已经提出了这个问题。不幸的是,他们没有谈论绑定数据(或者至少我不明白要让它发挥作用还需要做什么......)

有没有人可以给我一些提示? 我已经使用 ILSpy 来了解如何完成这项工作,但这对我来说比它更有帮助更令人困惑。

【问题讨论】:

  • 你的意思是 HTML 格式?你看到 、 、 了吗?您的 DataGridView 究竟是什么样的?
  • 我想要一个标准的 DataGridView 控件(即不是基于 Web,而是在标准表单应用程序中)。在此控件中,有一列应显示 HTML 格式的文本。我不希望那里有任何 s s 或 s(尽管如果它们在那里应该显示它们),我宁愿显示一些文本格式,例如
      、 等等。 HTML 文本已存在于现有数据库中,应在此 DataGridView 列中正确显示...

标签: c# html winforms datagridview


【解决方案1】:

为在数据网格视图中呈现 html 内容提供了一种简化的解决方案。

解决方案使用WebBrowser渲染html并将内容转换为位图,然后使用图形绘制到单元格中。

using System;
using System.Drawing;
using System.Windows.Forms;

namespace DataGridViewTest
{
internal class DataGridViewHtmlCell : DataGridViewTextBoxCell
{
    protected override void Paint(
        Graphics graphics,
        Rectangle clipBounds,
        Rectangle cellBounds,
        Int32 rowIndex,
        DataGridViewElementStates cellState,
        Object values,
        Object formattedValue,
        String errorText,
        System.Windows.Forms.DataGridViewCellStyle cellStyle,
        DataGridViewAdvancedBorderStyle advancedBorderStyle,
        DataGridViewPaintParts paintParts)
    {
        // add a condition here to check formattedValue is Html
        // you shall use HtmlAgilityPack to determine this.
        if(isHtml)
        {
            RenderHtmlValue(graphics, cellBounds, formattedValue, true);
        }
        else
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, values, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
        }
    }   

    private Size RenderHtmlValue(
        Graphics graphics,
        Rectangle cellBounds,
        string formattedValue,
        bool drawCell)
    {
        using (var webBrowser = new WebBrowser())
        {
            webBrowser.ScrollBarsEnabled = false;

            webBrowser.Navigate("about:blank");
            webBrowser.Document.Write(formattedValue);
            webBrowser.Size = Size;

            var rect = new Rectangle(
                webBrowser.Document.Body.ScrollRectangle.Location,
                webBrowser.Document.Body.ScrollRectangle.Size);

            rect.Width = Size.Width - cellBounds.X;
            webBrowser.Size = rect.Size;

            cellBounds = new Rectangle(cellBounds.X, cellBounds.Y, rect.Width, rect.Height);

            var htmlBodyElement = webBrowser.Document.Body.DomElement as mshtml.IHTMLBodyElement;
            htmlBodyElement.WhenNotNull(
                bodyElement =>
                {
                    cellBounds.Height += Convert.ToInt32(htmlBodyElement.bottomMargin);
                });

            if(drawCell)
            {
                using (var bitmap = new Bitmap(webBrowser.Width, webBrowser.Height))
                {
                    webBrowser.DrawToBitmap(bitmap, targetBounds);
                    graphics.DrawImage(bitmap, location);
                }
            }
        }

        return cellBounds.Size;
    }

    protected override Size GetPreferredSize(
        Graphics graphics,
        System.Windows.Forms.DataGridViewCellStyle cellStyle,
        Int32 rowIndex,
        Size constraintSize)
    {
        return RenderHtmlValue(graphics, cellBounds, formattedValue, false);
    }
}

}

【讨论】:

  • 这当然可以,但这不是我想要实现的。但我想我想做的事情根本不可能使用内置函数。不过,感谢您的回答。
猜你喜欢
相关资源
最近更新 更多
热门标签