Search code examples
htmlasp.netgridviewitemtemplate

asp.net display values under one another in item template of gridview


I want the values in the cell generated from an item template to be displayed below one another in a list, rather than horizontally. How text should look in cell

<asp:GridView ID="dgv" runat="server">
        <Columns>
            <asp:TemplateField ShowHeader="true"  ItemStyle-Wrap="true" ItemStyle-Width="50px">
                <ItemTemplate>
                    <%# Eval("Name") + Environment.NewLine + Eval("Age")+ Environment.NewLine + Eval("lang")+ Environment.NewLine + Eval("Town")%>

                </ItemTemplate>


                    </asp:TemplateField>                
            </Columns>
        </asp:GridView>

Solution

  • Got what I wanted by placing a html table within the item template

    <asp:TemplateField ShowHeader="true"  ItemStyle-Wrap="true" ItemStyle-Width="50px">
                    <ItemTemplate>
                        <%--<%# Eval("Name") + " " + Eval("Age")+ " " + Eval("lang")+ " " + Eval("Town")%>--%>
                        <table style="width: 100%;">
                            <tr>
                                <td><%# Eval("Name")%></td>
                            </tr>
                            <tr>
                                <td><%# Eval("Age")%></td>
                            </tr>
                            <tr>
                                <td><%# Eval("lang")%></td>
                            </tr>
                            <tr>
                                <td><%# Eval("Town")%></td>
                            </tr>
                        </table>
                    </ItemTemplate>