Hi trisetia302,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="OnItemDataBound">
<HeaderTemplate>
<table style="width: 100%;" id="myTable" class="table table-hover table-responsive table-borderless">
<thead class="table table-dark text-center">
<tr>
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Phone Number</th>
<th>Join Date</th>
<th>Country</th>
<th>#</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr runat="server" id="row">
<td class="text-center">
<%#Eval("EmployeeID")%>
</td>
<td style="width: 25%;" class="text-center">
<%#Eval("FirstName")%>
</td>
<td style="width: 25%;" class="text-center">
<%#Eval("Address")%>
</td>
<td class="text-center">
<%#Eval("HomePhone")%>
</td>
<td class="text-center">
<%#Eval("BirthDate","{0: dd MMMM yyyy}")%>
</td>
<td style="width: 10%;">
<%#Eval("Country")%>
</td>
<td>
<asp:LinkButton ID="LinkButton1" CommandArgument='<%#Eval("EmployeeID") %>'
class="fa fa-edit btn btn-warning btn-sm" runat="server"> Detail</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI.HtmlControls
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindRepeater();
}
}
protected void BindRepeater()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand sqlcmd = new SqlCommand())
{
sqlcmd.Connection = con;
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = "SELECT * FROM Employees";
using (SqlDataAdapter sda = new SqlDataAdapter(sqlcmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
}
}
}
protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string HtmlStringAktif = "text-center bg bg-success";
string HtmlStringTidakAktif = "text-center bg bg-danger";
HtmlTableRow row = e.Item.FindControl("row") as HtmlTableRow;
if ((e.Item.DataItem as DataRowView)["Country"].ToString() == "USA")
{
row.Attributes["class"] = HtmlStringAktif;
}
else
{
row.Attributes["class"] = HtmlStringTidakAktif;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
BindRepeater()
End If
End Sub
Protected Sub BindRepeater()
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using sqlcmd As SqlCommand = New SqlCommand()
sqlcmd.Connection = con
sqlcmd.CommandType = CommandType.Text
sqlcmd.CommandText = "SELECT * FROM Employees"
Using sda As SqlDataAdapter = New SqlDataAdapter(sqlcmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Repeater1.DataSource = dt
Repeater1.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub OnItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim HtmlStringAktif As String = "text-center bg bg-success"
Dim HtmlStringTidakAktif As String = "text-center bg bg-danger"
Dim row As HtmlTableRow = TryCast(e.Item.FindControl("row"), HtmlTableRow)
If (TryCast(e.Item.DataItem, DataRowView))("Country").ToString() = "USA" Then
row.Attributes("class") = HtmlStringAktif
Else
row.Attributes("class") = HtmlStringTidakAktif
End If
End If
End Sub
Screenshot