how to change listbox item color based on condition from datatable c#

Shagil A 1 Reputation point
2021-10-17T11:56:06.82+00:00

Hi Team,

I have values in DataTable from DB in the following way

GID GName GCnt
1 Test1 2
2 Test2 1
3 Test3 2
4 Test4 1

In my list box i have to display the data and color the items based on GCnt value, If GCnt is 2 then red color and if its 1 then Yellow color. DataTextField should be GName , DataValueField should be the GID. Based on the selection i have to get the GID. Can someone help me please

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,715 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,288 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,308 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Albert Kallal 4,806 Reputation points
    2021-10-17T22:18:39.493+00:00

    Hum, ok, assume a list box list like this:

    141156-image.png

    Then code to fill above like this:

    Sub LoadData()  
        Using conn As New SqlConnection(My.Settings.TEST4)  
            Using cmdSQL As New SqlCommand("SELECT GID, GName, GCnt from Products ORDER BY GName", conn)  
                conn.Open()  
                Dim rst As New DataTable  
                rst.Load(cmdSQL.ExecuteReader)  
      
                ListBox1.DataSource = rst  
                ListBox1.DataBind()      
                Dim iRow = 0  
                For Each lRow As ListItem In ListBox1.Items  
                    Dim sHex As String = Hex(rst.Rows(iRow).Item("GCnt")).PadLeft(6, "0")  
                    lRow.Attributes.Add("style", "background-color:#" & sHex)  
                    iRow += 1  
                Next  
            End Using  
        End Using  
    End Sub  
      
    

    and the output is this:

    141138-image.png

    And our data table was this:

    141085-image.png

    Regards,
    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada

    0 comments No comments

  2. Lan Huang-MSFT 25,876 Reputation points Microsoft Vendor
    2021-10-18T10:22:48.88+00:00

    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:
    141295-1.png
    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.

    0 comments No comments