【问题标题】:How Can we Render Html text in DataGridView?我们如何在 DataGridView 中呈现 Html 文本?
【发布时间】:2013-06-11 12:12:28
【问题描述】:

我有 html 文本数据,我想在我的 gridview 单元格中绑定这些数据。

但我的 datagridview 不是格式化的 html 文本。

exp:

string htmltext = "<p>hi some text here</p>";
dgrow.rows[0].cell[1].value=htmltext;

在这种情况下,我的 gridview 的单元格值也包含 Html 标记。 那么如何在我的网格视图中格式化 Html 文本?

【问题讨论】:

  • 来自 WebForms 的 GridView?
  • 我正在使用 win 表单而不是 web。
  • This link 对你有用..

标签: c# winforms datagrid


【解决方案1】:

我最近还需要在 WinForms DataGridView 控件中呈现 HTML 文本。

和你一样,我找不到任何适合我需要的东西,所以创建了我自己的自定义 DataGridViewHTMLCell 类。

您可以在这里找到代码和示例项目:https://github.com/OceanAirdrop/DataGridViewHTMLCell

【讨论】:

    【解决方案2】:

    在标记中(请注意 Mode 是 PassThrough):

    <asp:GridView ID="GridView1" runat="server">
      <Columns>
       <asp:TemplateField>
         <ItemTemplate>
           <asp:Literal ID="literal1" Mode="PassThrough" runat="server"></asp:Literal>
         </ItemTemplate>
      </asp:TemplateField>
     </Columns>
    

    使用 RowDataBound 事件查找字面量并设置值:

    void GridView1GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
    { 
      if(e.Row.RowType == DataControlRowType.DataRow)
      {
        //find the literal
        var literal1 = e.Item.FindControl("literal1") as Literal;
        if (literal1 != null)
        {
          literal1.Text = "<p>hi some text here</p>";
        }
       }
      }
    

    如有错误请见谅,以上我没有测试过。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-18
      • 2013-03-09
      • 1970-01-01
      • 2011-09-09
      • 2018-11-14
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      相关资源
      最近更新 更多