Add "th" to row in gridview

I am trying to achieve the below HTML in a Gridview but not getting the results i require

<tbody>
    <tr>
        <th element="row">Name</th>
        <td>Data 1 </td>
        <td>Data 2 </td>
        <td>Data 3</td>
    </tr>
    <tr></tr>
<tbody>

Im struggling with <th element="row">Name</th> area. In my OnRowDataBound (removed some code for clarity) I have the below

protected void gv1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        TableCellCollection cell = e.Row.Cells;
        cell[0].Attributes.Add("th", "element");
    }
}

but this produces the below HTML

<td th="element">

Other rows seem to be ok.

My inline code is

<Columns>
    <asp:TemplateField HeaderText="Name">
         <ItemTemplate>
              <asp:Label ID="label1" runat="server"></asp:Label>
         </ItemTemplate>
     </asp:TemplateField>
 ....

How could i achieve the required HTML?

2 answers

  • answered 2020-03-31 10:10 Steven

    <th> needs to be defined on an unique <tr>, because the <tr> means the row.

    Example shown here: https://www.w3schools.com/html/html_tables.asp

  • answered 2020-03-31 11:09 VDWWD

    Use the following code to make the header row th

    protected void gv1_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.TableSection = TableRowSection.TableHeader;
        }
    }