2

I'm trying to display records within my GridView and a few of the cells contain text with carriage returns. However when this text is displayed the carriage returns are gone. Is there an easy way to display carriage returns in a GridView Cell?

I've tried using a TextBox within the cell which works but I then have the scollbars of the TextBox. I'm not too keen on this way either as the textbox may render differently in different browser.

My code is displayed below, I've left my testing in there so you can see all the ways I've tried:

<asp:GridView ID="gridResults" runat="server"  AutoGenerateColumns="false" CellSpacing="-1" 
            CssClass ="stretch separate" GridLines="None" EnableViewState="true">
            <HeaderStyle CssClass="header pad" />
            <RowStyle CssClass="row pad"/>                
            <EmptyDataRowStyle CssClass="empty pad" />
            <Columns>
                <asp:BoundField DataField="Notes" HeaderText="Notes"  HtmlEncode="true">
                    <ItemStyle Width="100"   />
                </asp:BoundField>
                <asp:TemplateField HeaderText="Notes" >
                    <ItemTemplate>
                        <asp:Label ID="test" runat="server"   Text='<%# Bind("Notes") %>' />
                        <asp:TextBox ID="textNotes" runat="server" CssClass="right"  TextMode="MultiLine" Text='<%# Bind("Notes") %>' BorderStyle="None" />
                    </ItemTemplate>                        
                    <ItemStyle Width="50" CssClass="right"  />
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

I'm using ASP.Net 4.0 and c#

Thanks

user1131661
  • 229
  • 3
  • 8
  • 19

1 Answers1

6

Try setting the Text attribute of the <asp:Label> to Text='<%# Eval("Notes").ToString().Replace("\n", "<br />") %>'.

pete
  • 24,141
  • 4
  • 37
  • 51
  • Given that you are adding overhead to the page processing, yes it will (technically speaking). Whether that slowdown is noticeable or not depends on how many records you are returning in the GridView and the number of replacements needed in the `Notes` field. – pete Feb 08 '12 at 11:58