'am try to use thead and tbody with gridview in asp.net
I want to know how to use thead and tbody in gridview in asp.net and this my html am trying to add page control presented by DataTables I have to use thead and tbody with datatables but in gridview i don't know how or where I have to put thead and tbody because in gridview there are boundfield include data field and the header text
here is the template field with a link
<asp:TemplateField HeaderText="تعديل">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="EditRecored" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>' >تعديل</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
here is the bound field include the header text and the data field
<asp:BoundField DataField="Phone"
HeaderText="رقم الهاتف " ItemStyle-Font-Size="Small">
<asp:BoundField DataField="AddressEn"
HeaderText="العنوان انجليزي" ItemStyle-Font-Size="Small">
<asp:BoundField DataField="NameEn"
HeaderText="اسم الحساب انجليزي" ItemStyle-Font-Size="Small">
<asp:BoundField DataField="ledgerCode"
HeaderText="رقم الحساب الرئيسي" ItemStyle-Font-Size="Small">
</Columns>
<RowStyle BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" />
<AlternatingRowStyle BackColor="#DDDDDD" />
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="#aaaaaa" Font-Bold="True" HorizontalAlign="Center"
VerticalAlign="Middle" ForeColor="Black" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px"/>
</asp:GridView>
Solution 1:[1]
To add thead and tbody element try this:
GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
GridView1.HeaderRow.CssClass = "yourClassName";
Solution 2:[2]
Unfortunately, you cannot have custom thead
and tfoot
or specify it as static. Here is a workaround where you will have a table
, htead
, tfoot
, then a tbody
contains a placeholder
. The placeholder will be filled by rows manually in code.
The code will be something like this:
<table class="table table-striped" style="border-collapse: collapse;">
<thead>
<!-- whatever you would like to have here -->
</thead>
<tbody>
<asp:PlaceHolder ID="placeholder" runat="server" />
</tbody>
<tfoot>
<!-- whatever you would like to have here -->
</tfoot>
</table>
and here is the C# code:
private void fillTBody(DataTable dt)
{
//Building an HTML string.
StringBuilder html = new StringBuilder();
//Building the Data rows.
foreach (DataRow row in dt.Rows)
{
html.Append("<tr>");
foreach (DataColumn column in dt.Columns)
{
html.Append("<td>");
html.Append(row[column.ColumnName]);
html.Append("</td>");
}
html.Append("</tr>");
}
////Append the HTML string to Placeholder.
placeholder.Controls.Add(new Literal { Text = html.ToString() });
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Aria |
Solution 2 | Abdulrazak Zakieh |