Search code examples
c#asp.netsql-serverasprepeater

How get data from database column as a heading using Repeater in ASP.NET


image The Upper portion of present my database table structure and lower portion that in which form i want to show data.

<table class="table table-bordered table-hover">
  <thead>
    <tr class="text-center">
      <th>Course Code</th>
      <th>Subject</th>
      <th>Cr.Hours</th>
      <th>Grade</th>
    </tr>
    <tr>
      <td colspan="4">
        <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
      </td>
    </tr>
  </thead>
  <tbody>

    <asp:Repeater ID="outer" runat="server">

      <ItemTemplate>
        <tr>
          <td>
            <asp:Label Text='<%#Eval("[SpringFall]")%>' Style="text-align: center; display: none; font-size: 18px; font-family: 'Times New Roman'" ID="Label4" runat="server" Font-Bold="true"> </asp:Label>

          </td>
          <td>
            <asp:Label ID="AllCcode" runat="server" Text='<%#Eval("[Course_Code]") %>'></asp:Label>
          </td>
          <td>
            <asp:Label ID="AllSubject" runat="server" Text='<%#Eval("[Subject]") %>'></asp:Label>
          </td>
          <td>
            <asp:Label ID="AllCrHr" runat="server" Text='<%#Eval("[Credit_Hours]") %>'></asp:Label>
          </td>
          <td>
            <asp:Label ID="AllGrade" Font-Bold="true" runat="server" Text='<%#Eval("[Total_Marks]") %>'></asp:Label>
          </td>
        </tr>
      </ItemTemplate>
    </asp:Repeater>
  </tbody>
</table>

Here Is my code..bind repeater on button click event using c# with simple sql SELECT query.Any query for bind repeater with more efficient working and show data in given form.

protected void AttendencBtn_Click(object sender, EventArgs e)
{
  //  MultiView1.ActiveViewIndex = 8;
    conn.Open();
    sda = new SqlDataAdapter("SELECT * FROM  Attendence where Roll_Number='" + Session["RollNumb"] + "' ", conn);
    dt = new DataTable();
    sda.Fill(dt);
    AttendencRpt.DataSource = dt;
    AttendencRpt.DataBind();
    conn.Close();
}

Solution

  • I think you are looking for something like this. First create a public variable in code behind.

    public int currentYear = 2000;
    
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    

    Then change the Repeater to something like this:

    <table border="1">
        <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
    
                <%# Convert.ToDateTime(Eval("myYear")).Year != currentYear && Container.ItemIndex > 0 ? "<tr><td colspan=\"5\">" + Eval("myYear") + "</td></tr>" : "" %>
    
                <tr>
                    <td>
                        normal rows here
                    </td>
                </tr>
    
                <%# currentYear = Convert.ToDateTime(Eval("myYear")).Year %>
    
            </ItemTemplate>
        </asp:Repeater>
    </table>
    

    What will happen is that the value of currentYear is compared to the current row value. If it does not match a new row will be created. Then the value of currentYear is updated to be checked in the next row bound to the Repeater.