-1

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?

VDWWD
  • 35,079
  • 22
  • 62
  • 79
Computer
  • 2,149
  • 7
  • 34
  • 71

2 Answers2

0

<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

Steven
  • 1,996
  • 3
  • 22
  • 33
0

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;
    }
}
VDWWD
  • 35,079
  • 22
  • 62
  • 79