Customizing GridView Header Text for Destination

I have a GridView that has columns such as:

| A | BC | DE / F | 

I want them to be wrapped in a certain way - that is, I do not want to leave it in the browser to determine whether to wrap or not, depending on the width of the column. Therefore, in the above example, I may need the following:

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

I tried using \n as well as using <br/> , but both of them did not work.

Any ideas?

+4
source share
2 answers

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

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 constructor instead of directly editing aspx, it will change your " < " to " &lt; " when you click OK.

+6
source

If you use the template field, you can have fine grain control over the contents of the header in the header template:

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

All Articles