如何在从DataTable生成的GridView中以html格式显示文本

时间:2014-03-10 13:25:25

标签: c# asp.net gridview datatable

我有一种创建DataTable的方法:

private DataTable GetCssPropertiesDataTable()
{
    DataTable dataTable = new DataTable();

    DataColumn column;
    DataRow row;

    column = new DataColumn();
    column.DataType = typeof(string);
    column.ColumnName = "Property";
    column.Caption = "Property";
    dataTable.Columns.Add(column);

    column = new DataColumn();
    column.DataType = typeof(double);
    column.ColumnName = "Value";
    column.Caption = "Value";
    dataTable.Columns.Add(column);

    column = new DataColumn();
    column.DataType = typeof(string);
    column.ColumnName = "Unit";
    column.Caption = "Unit";
    dataTable.Columns.Add(column);

    row = dataTable.NewRow();
    row["Property"] = "A";
    row["Value"] = A;
    row["Unit"] = "m<sup>2</sup>";
    dataTable.Rows.Add(row);

    return dataTable;
}

gridview(aspx):

<asp:GridView ID="cssPropertiesGridView" runat="server" CssClass="gridView"></asp:GridView>

和代码behing中的数据绑定(aspx.cs):

cssPropertiesGridView.DataSource = calculator.CssPropertiesDataTable;
cssPropertiesGridView.DataBind();

生成的网页上的结果:

m<sup>2</sup>

有没有办法显示 m 2

1 个答案:

答案 0 :(得分:2)

您应该为单位列(在datagrid中)指定属性HtmlEncode =“false”,如下面的代码段所示,以便显示html

<Columns>
    <asp:BoundField DataField="Property" />
    <asp:BoundField DataField="Value" />
    <asp:BoundField DataField="Unit" HtmlEncode="False" />
</Columns>

希望它有所帮助。