locked
How to render Gridview without tbody ? RRS feed

  • Question

  • User-775831949 posted

    The standard Gridview renders <tbody> tag on html.

    How to remove it ? Thanks

    Sunday, January 21, 2018 1:27 PM

Answers

  • User-707554951 posted

    Hi hkbeer

    You could remove<tbody> tag from table by using Jquery..

    Working code as below:

     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script>
            $(document).ready(function () {  
                var html=$("#GridView1").html();
                $("#GridView1").html($("#GridView1").html().replace("<tbody>", "").replace("</tbody>", ""));
            });
        </script>
       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="Id">
                    <Columns>
                        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
                        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="100" />
                    </Columns>
                </asp:GridView>

    CodeBehind:

       if (!this.IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Id"), new DataColumn("Group"), new DataColumn("Name"), new DataColumn("Country") });
                    dt.Rows.Add(1, "A", "John Hammond", "United States");
                    dt.Rows.Add(2, "B", "Mudassar Khan", "India");           
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }

    Output:

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 22, 2018 7:26 AM