p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ADO.NET (http://p2p.wrox.com/forumdisplay.php?f=109)
-   -   listbox, different color for each item? (http://p2p.wrox.com/showthread.php?t=5722)

MichaelTJ November 3rd, 2003 08:08 PM

listbox, different color for each item?
 
Hi all,
I was just wondering if it's possible to change the text color or background color for an item in a listbox? Eks. you have an SQL querry string that gets some values. If one of the values is 1 then the color is normal. If the value is 2 then the color turns red! Is this possible?

***************MY CODE FOR FILLING THE LISTBOX********
    Sub FillList(ByVal Level As Int32, ByVal SQL1 As String)
        Dim SQ As Data.SqlClient.SqlDataReader
        Dim SQLConnect As Data.SqlClient.SqlConnection
        SQLConnect = SQL.Connect
        Dim SQLCmd As New System.Data.SqlClient.SqlCommand("set dateformat dmy", SQLConnect)
        ListColl(Level).Items.Clear()
        Try
            SQLCmd.CommandText = (SQL1)
            SQ = SQLCmd.ExecuteReader()
            Dim i As Integer
            Do While SQ.Read = True
                If SQ.GetString(0).Trim <> "" Then ListColl(Level).Items.Add(SQ.GetString(0))
            Loop
            SQ.Close()
        Catch ex As Exception
            Response.Write("Fill list: " & ex.Message)
        End Try
        SQLCmd.Connection.Close()
        SQLConnect.Close()
    End Sub
************END CODE*********************

I need something like this:
************CODE FROM ABOVE*********
Do While SQ.Read = True
If SQ(2) = 1 then
If SQ.GetString(0).Trim <> "" Then ListColl(Level).Items.Add(SQ.GetString(0))
else
*Some Color setting for that item*
If SQ.GetString(0).Trim <> "" Then ListColl(Level).Items.Add(SQ.GetString(0))
end if
Loop
***************END******************

Thanks for any help I can get on this!

------------------------
All help is Good help!
Regards
Michael

KenSchaefer November 13th, 2003 10:37 PM

Create a new ListItem. Set the cssClass to the appropriate CSS class that you want to use. Then add this ListItem to your DropDownList's Items collection. So you should end up with:

<option class="itemA">Some Value</option>
<option class="itemB">Some Other Value</option>

and then in your CSS file (or inline) you have:

itemA
{
   color: #FF0000;
}
itemB
{
   color: #00FF00;
}

Cheers
Ken



Microsoft MVP - Windows Server (IIS)
www.adOpenStatic.com


All times are GMT -4. The time now is 02:34 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.