Ashes
Ashes

Reputation: 65

C#- Line break instead of comma-separated-values in GridView

I have a DataTable which is populated from database. In one of its column i have comma separated values. I want to remove comma and put the line break so that when i bind it to the GridView it shows line breaks instead of comma seperated values.

Upvotes: 1

Views: 3926

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460158

A simple approach for an ASP.NET GridView:

<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
    <Columns>
    <asp:TemplateField HeaderText="Values">
        <ItemTemplate>
            <asp:Literal runat="server" id="Values" 
                Text='<%# string.Join("<br />", Eval("Values").ToString().Split(new []{","},StringSplitOptions.None)) %>'>
            </asp:Literal>
        </ItemTemplate>
    </asp:TemplateField>
    </Columns>
</asp:GridView>

Bound a DataTable with some numbers to it, here's the result:

enter image description here

Of yourse it's also possible to use a nested GridView and bind it in RowDataBound of the outer grid. But that's a more difficult approach with some drawbacks which might be too much in this case.

Upvotes: 1

Pandian
Pandian

Reputation: 9126

Try the below...

If it is windows Application then try the below...

yourString = yourString.Replace(",",System.Environment.NewLine)

Upvotes: 2

Related Questions