Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old November 3rd, 2003, 08:08 PM
Authorized User
Join Date: Oct 2003
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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)
            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))
        Catch ex As Exception
            Response.Write("Fill list: " & ex.Message)
        End Try
    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))
*Some Color setting for that item*
If SQ.GetString(0).Trim <> "" Then ListColl(Level).Items.Add(SQ.GetString(0))
end if

Thanks for any help I can get on this!

All help is Good help!
All help is Good help!
Old November 13th, 2003, 10:37 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 111
Thanks: 0
Thanked 0 Times in 0 Posts

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:

   color: #FF0000;
   color: #00FF00;


Microsoft MVP - Windows Server (IIS)

Similar Threads
Thread Thread Starter Forum Replies Last Post
change only the first item in array's back color i remya1000 .NET Framework 1.x 1 November 5th, 2007 10:43 PM
ListBox Item selection caterpillar ASP.NET 1.0 and 1.1 Professional 1 July 26th, 2006 12:50 PM
Select an particular item when a listbox opens? alastair Access 2 December 29th, 2005 06:04 PM
One item in Listbox venterjo General .NET 2 February 1st, 2005 05:45 AM
How to get selected Item from Listbox? Iceman571 ADO.NET 1 March 25th, 2004 06:33 AM

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