C# 将 GridView 标题文本设置为按指示换行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/351903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:11:34  来源:igfitidea点击:

Setting GridView header text to wrap as directed

c#asp.nethtmlgridview

提问by

I have a GridView that has columns such as:

我有一个 GridView,其中包含以下列:

|    A    |    B C    |    D E / F   |

I want these to be wrapped in a particular way - that is, I do not want to leave it up to the browser to work out whether to wrap or not depending on the column width. So in the above example I may want the following:

我希望这些以特定方式包装 - 也就是说,我不想让浏览器根据列宽来确定是否换行。所以在上面的例子中,我可能想要以下内容:

|    A    |    B      |    D         |
|         |    C      |    E / F     |

I have tried using \nand also using <br/>however both of these did not work.

我曾尝试使用\n和使用,<br/>但是这两种方法都不起作用。

Any ideas?

有任何想法吗?

采纳答案by Kelly Adams

You can do it without templates. Just set HtmlEncode="False" on the headers with <br />tags in them.

您可以在没有模板的情况下完成。只需在带有<br />标签的标题上设置 HtmlEncode="False"即可。

Example:

例子:

<asp:GridView ID="GridView1" runat="server" DataSourceID="Data">
<Columns>
    <asp:BoundField HeaderText="First Line<br />Second Line" DataField="ContactID"
                HtmlEncode="False" />
    <asp:BoundField HeaderText="Second" DataField="FirstName" />
    <asp:BoundField HeaderText="Third<br />Extra" DataField="Title" />
</Columns>
</asp:GridView>

Renders:

渲染:

First Line  |   Second  |  Third<br />Extra |
Second Line |           |                   |
---------------------------------------------
1           | Gustavo   | Mr.               |
---------------------------------------------
2           | Catherine | Ms.               |
---------------------------------------------

NOTE: If you use the Designer rather than editing the aspx directly, it will change your "<" into "&lt;" when you click OK.

注意:如果您使用设计器而不是直接编辑 aspx,当您单击“确定”时,它会将您的“ <”更改为“ &lt;”。

回答by HectorMac

If you use a template field, you can have fine grain control the header content in the header template:

如果您使用模板字段,则可以对标题模板中的标题内容进行细粒度控制:

<asp:templatefield>
    <headertemplate>
      D<br />
      E / F
    </headertemplate>
    <itemtemplate>
        <%#Eval("MyField")%>
    </itemtemplate>
</asp:templatefield>