Make a new line in a GridView

2019-09-03 12:40发布

问题:

I've an ASP.Net 2.0 Gridview control that I can bind a List<T> too, for a specific field in List<T> like "Old" and "New" I want to place new line characters that will break the text into separate lines e.g.:

Column1=Value1 Column2=Value2

to become:

Column1=Value1
Column2=Value2

回答1:

I guess there are two ways to do this:

  1. Change the way your List is formatted, using string concatenation to join the two columns into one field (something like: column1 + '<br>' + column2). If you're using SQL, for example, you can use a query with string concatenation (more info)

  2. Use the gridview's events: OnDataBound and\or OnRowDataBound to change how data is viewed in the gridview control. (more info)



回答2:

You could convert the Gridview column to a templated column, and then do your special formatting there.

<asp:TemplateField HeaderText="Title">
   <ItemTemplate>
      Column 1 : <%# Eval("Column1") %> <br />
      Column 2 : <%# Eval("Column2") %>
   </ItemTemplate>
</asp:TemplateField>