AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=1263&pId=-1
CodeSnip: Changing the Color of a List Item's Text at Run-Time with Visual Basic 2005
page
by Shaun Eutsey
Feedback
Average Rating: 
Views (Total / Last 10 Days): 24974/ 82

Introduction

Today has been one of "those" days.  A co-worker asks for something that sounds easy, but in reality it becomes larger than it ever should have been.  That, my friends, is a recipe for a migraine and a learning opportunity.

The issue that was brought to me was that there are "Active" and "Inactive" items in a DropDownList that needed to be distinct within the list.  They need to be in the same list, but the inactive items need to stand out.

This started me on my fated journey.  I hit Google with a vengeance.  After all, it would be easier to find a solution from someone who has done it before than it would be to create my own, why recreate the wheel, right?

After being denied any good advice (though some of the failed examples that I ran across helped to point me in the right direction), I finally put it to myself to perform this task on my own. 

Let me start out by explaining the data that I am using.  I will be using the Territories table from the Northwood database.  I will be using the RegionID field to determine the color of the text.

Call to the database.

Listing 1

Dim CONNECTION_STRING As String = _
 "Data Source=(local);Initial Catalog=Northwind;Integrated Security=True"
 
Dim sQuery As String = _
 "select TerritoryID, TerritoryDescription, RegionID from Territories"
 
Dim dr As SqlDataReader
Dim cn As New SqlConnection(CONNECTION_STRING)
Dim cmd As New SqlCommand(sQuery, cn)

The fun begins when we actually call the cmd.executereader.

Listing 2

cn.Open()
dr = cmd.ExecuteReader
 
Do While dr.Read
    Dim li As New ListItem _
 (dr("TerritoryDescription"), dr("TerritoryID"))
    Select Case dr("RegionID")
    Case 1
      li.Attributes.Add("style", "color:red")
    Case 2
      li.Attributes.Add("style", "color:Green")
    Case 3
      li.Attributes.Add("style", "color:Dodgerblue")
    Case Else
      li.Attributes.Add("style", "color:orange")
    End Select
    ddlTerritories.Items.Add(li)
Loop
cn.Close()
Changing the Style

By adding the "style" attribute to the list item, we are in effect changing the style on every member of the DropDownList as we are adding them.  Granted, this method will not work with data binding, but there is no performance benefit for data binding anyway.

As the next graphic illustrates, this method delivers the desired result.

Figure 1

Downloads
Conclusion

Even though I was beset by frustration in trying to find this solution, in the end, the code itself was not that difficult.  It is my hope that this example will save you time and frustration while helping you to deliver a smooth and effective solution to your end users.

 


Product Spotlight
Product Spotlight 

©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-09-21 5:45:46 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search