Hi @Shagil A ,
First, you can bind the listbox to the database, and then match the value of Gcnt according to the database, and then change the color of the row corresponding to the listbox.
The code:
WebForm1.aspx
<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBox1" runat="server"
DataTextField="GName"
DataValueField="GID">
</asp:ListBox>
</div>
</form>
WebForm1.aspx.cs
SqlConnection conn = new SqlConnection(***);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dts = new DataTable();
string str = "select * From GTable";
SqlDataAdapter adp = new SqlDataAdapter(str, conn);
adp.Fill(dts);
ListBox1.DataSource = dts;
ListBox1.DataBind();
for(int i = 0; i < ListBox1.Items.Count; i++)
{
var GCnt = dts.Rows[i].ItemArray[2];
var a = ListBox1.Items[i];
if (GCnt.ToString() == "1")
{
a.Attributes.Add("style", "background-color:red");
}
else
{
a.Attributes.Add("style", "background-color:yellow");
}
}
}
}
The Result:
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.