Q. How to populate similar DropDownLists?

joe_pool_is

Contributor
Joined
Jan 18, 2004
Messages
507
Location
Longview, TX [USA]
I am putting together a site with several identical DropDownLists. Particularly, I have 4 different STATE lists.

Instead of adding all of the US states [i.e. StateList1.Item.Add("TEXAS")] for each DropDownList, I am looking for a more elegant approach.

I tried defining a class, then declaring the DropDownList as the new class. Obviously, that did not work.

Could someone describe an elegant way to approach this?

Thanks in advance to those who respond.
 
If all you care about is the text for each state you could simply create an ArrayList of the states, then bind it to your dropdowns.

Code:
ArrayList oStates = new ArrayList();

//   Build up list of states.
oStates.Add("Arizona");
oStates.Add("California");
oStates.Add("Washington");
//  Etc.

//  Bind the list to the drop downs.
ddlStates1.DataSource = oStates;
ddlStates1.DataBind();
ddlStates2.DataSource = oStates;
ddlStates2.DataBind();
//  Etc.

That's kind of a simple example. Usually things are a bit more complicated - like you need to bind a collection of structs or objects that contain a state list with state text and associated ID numbers or whatever from a database or something.

I could provide a more complex example but the principle is the same regardless of the complexity - you bind some type of collection-oriented datasource to your dropdown and this enables you to re-use the same data source as much as you want.

If you find yourself using the same data source in more than one page you could also put it in it's own class...or have a class of common lists...that you can tap into from as many pages as you like.

Paul
 
One more thing:

I am trying to develop this further. I am building a class called DDLClass.vb that I am populating with different data variables (List of US States, Months of the Year, Days of the Month, Years, etc.). I am designing this in such a way that I can use it over and over.

Code:
[size=1][color=#0000ff]Public[/color][/size][size=1] [/size][size=1][color=#0000ff]Class[/color][/size][size=1] DropListData
[/size][size=1][color=#0000ff]Dim[/color][/size][size=1] oData [/size][size=1][color=#0000ff]As[/color][/size][size=1] [/size][size=1][color=#0000ff]New[/color][/size][size=1] ArrayList

[/size][size=1][color=#0000ff]Private[/color][/size][size=1] [/size][size=1][color=#0000ff]Enum[/color][/size][size=1] DropListOptions

enmStates

enmMonths

enmDaysOfMonth

enmYears

[/size][size=1][color=#0000ff]End[/color][/size][size=1] [/size][size=1][color=#0000ff]Enum

[/color][/size][size=1][/size][size=1][color=#0000ff]Public[/color][/size][size=1] [/size][size=1][color=#0000ff]Sub[/color][/size][size=1] [/size][size=1][color=#0000ff]New[/color][/size][size=1]([/size][size=1][color=#0000ff]ByVal[/color][/size][size=1] intOption [/size][size=1][color=#0000ff]As[/color][/size][size=1] [/size][size=1][color=#0000ff]Integer[/color][/size][size=1])

[/size][size=1][color=#0000ff]Select[/color][/size][size=1] [/size][size=1][color=#0000ff]Case[/color][/size][size=1] intOption

[/size][size=1][color=#0000ff]Case[/color][/size][size=1] DropListOptions.enmStates

[/size][size=1][color=#0000ff]With[/color][/size][size=1] oData

.Add("AL - ALABAMA")

.Add("AK - ALASKA")

.Add("AR - ARKANSAS")

.Add("AZ - ARIZONA")

.Add("CA - CALIFORNIA")

.Add("CO - COLORADO")

.Add("CT - CONNECTICUT")

.Add("DC - DISTRICT OF COLUMBIA")

.Add("DE - DELAWARE")

.Add("FL - FLORIDA")

.Add("GA - GEORGIA")

.Add("HI - HAWAII")

.Add("ID - IDAHO")

.Add("IA - IOWA")

.Add("IL - ILLINOIS")

.Add("IN - INDIANA")

.Add("KS - KANSAS")

.Add("KY - KENTUCKY")

.Add("LA - LOUISIANA")

.Add("MA - MASSACHUSETTS")

.Add("MD - MARYLAND")

.Add("ME - MAINE")

.Add("MI - MICHIGAN")

.Add("MO - MISSOURI")

.Add("MN - MINNESOTA")

.Add("MS - MISSISSIPPI")

.Add("MT - MONTANA")

.Add("NC - NORTH CAROLINA")

.Add("ND - NORTH DAKOTA")

.Add("NE - NEBRASKA")

.Add("NH - NEW HAMPSHIRE")

.Add("NJ - NEW JERSEY")

.Add("NM - NEW MEXICO")

.Add("NY - NEW YORK")

.Add("NV - NEVADA")

.Add("OH - OHIO")

.Add("OK - OKLAHOMA")

.Add("OR - OREGON")

.Add("PA - PENNSYLVANIA")

.Add("PR - PUERTO RICO")

.Add("RI - RHODE ISLAND")

.Add("SC - SOUTH CAROLINA")

.Add("SD - SOUTH DAKOTA")

.Add("TN - TENNESSEE")

.Add("TX - TEXAS")

.Add("UT - UTAH")

.Add("VA - VIRGINIA")

.Add("VT - VERMONT")

.Add("WA - WASHINGTON")

.Add("WI - WISCONSIN")

.Add("WV - WEST VIRGINIA")

.Add("WY - WYOMING")

[/size][size=1][color=#0000ff]End[/color][/size][size=1] [/size][size=1][color=#0000ff]With

[/color][/size][size=1][/size][size=1][color=#0000ff]Case[/color][/size][size=1] DropListOptions.enmMonths

[/size][size=1][color=#0000ff]With[/color][/size][size=1] oData

.Add("01 - January")

.Add("02 - February")

.Add("03 - March")

.Add("04 - April")

.Add("05 - May")

.Add("06 - June")

.Add("07 - July")

.Add("08 - August")

.Add("09 - September")

.Add("10 - October")

.Add("11 - November")

.Add("12 - December")

[/size][size=1][color=#0000ff]End[/color][/size][size=1] [/size][size=1][color=#0000ff]With

[/color][/size][size=1][/size][size=1][color=#0000ff]Case[/color][/size][size=1] DropListOptions.enmDaysOfMonth

[/size][size=1][color=#0000ff]With[/color][/size][size=1] oData

.Add("01")

.Add("02")

.Add("03")

.Add("04")

.Add("05")

.Add("06")

.Add("07")

.Add("08")

.Add("09")

.Add("10")

.Add("11")

.Add("12")

.Add("13")

.Add("14")

.Add("15")

.Add("16")

.Add("17")

.Add("18")

.Add("19")

.Add("20")

.Add("21")

.Add("22")

.Add("23")

.Add("24")

.Add("25")

.Add("26")

.Add("27")

.Add("28")

.Add("29")

.Add("30")

.Add("31")

[/size][size=1][color=#0000ff]End[/color][/size][size=1] [/size][size=1][color=#0000ff]With

[/color][/size][size=1][/size][size=1][color=#0000ff]Case[/color][/size][size=1] DropListOptions.enmYears

[/size][size=1][color=#0000ff]With[/color][/size][size=1] oData

.Add("1900")

.Add("1901")

.Add("1902")

.Add("1903")

.Add("1904")

.Add("1905")

.Add("1906")

.Add("1907")

.Add("1908")

.Add("1909")

.Add("1910")

.Add("1911")

.Add("1912")

.Add("1913")

.Add("1914")

.Add("1915")

.Add("1916")

.Add("1917")

.Add("1918")

.Add("1919")

.Add("1920")

.Add("1921")

.Add("1922")

.Add("1923")

.Add("1924")

.Add("1925")

.Add("1926")

.Add("1927")

.Add("1928")

.Add("1929")

.Add("1930")

.Add("1931")

.Add("1932")

.Add("1933")

.Add("1934")

.Add("1935")

.Add("1936")

.Add("1937")

.Add("1938")

.Add("1939")

.Add("1940")

.Add("1941")

.Add("1942")

.Add("1943")

.Add("1944")

.Add("1945")

.Add("1946")

.Add("1947")

.Add("1948")

.Add("1949")

.Add("1950")

.Add("1951")

.Add("1952")

.Add("1953")

.Add("1954")

.Add("1955")

.Add("1956")

.Add("1957")

.Add("1958")

.Add("1959")

.Add("1960")

.Add("1961")

.Add("1962")

.Add("1963")

.Add("1964")

.Add("1965")

.Add("1966")

.Add("1967")

.Add("1968")

.Add("1969")

.Add("1970")

.Add("1971")

.Add("1972")

.Add("1973")

.Add("1974")

.Add("1975")

.Add("1976")

.Add("1977")

.Add("1978")

.Add("1979")

.Add("1980")

.Add("1981")

.Add("1982")

.Add("1983")

.Add("1984")

.Add("1985")

.Add("1986")

.Add("1987")

.Add("1988")

.Add("1989")

.Add("1990")

.Add("1991")

.Add("1992")

.Add("1993")

.Add("1994")

.Add("1995")

.Add("1996")

.Add("1997")

.Add("1998")

.Add("1999")

.Add("2000")

.Add("2001")

.Add("2002")

.Add("2003")

.Add("2004")

.Add("2005")

[/size][size=1][color=#0000ff]End[/color][/size][size=1] [/size][size=1][color=#0000ff]With

[/color][/size][size=1][/size][size=1][color=#0000ff]End[/color][/size][size=1] [/size][size=1][color=#0000ff]Select

[/color][/size][size=1][/size][size=1][color=#0000ff]End[/color][/size][size=1] [/size][size=1][color=#0000ff]Sub

End[/color][/size][size=1] [/size][size=1][color=#0000ff]Class[/color][/size]

Are my techniques above good or are there better ways?

Here is a snippet of my main .ASPX code:
Code:
[size=1]
[/size][size=1][color=#0000ff]Private[/color][/size][size=1] [/size][size=1][color=#0000ff]Sub[/color][/size][size=1] Page_Load([/size][size=1][color=#0000ff]ByVal[/color][/size][size=1] sender [/size][size=1][color=#0000ff]As[/color][/size][size=1] System.Object, [/size][size=1][color=#0000ff]ByVal[/color][/size][size=1] e [/size][size=1][color=#0000ff]As[/color][/size][size=1] System.EventArgs) [/size][size=1][color=#0000ff]Handles[/color][/size][size=1] [/size][size=1][color=#0000ff]MyBase[/color][/size][size=1].Load

[/size][size=1][/size][size=1][color=#0000ff]Dim[/color][/size][size=1] oStates [/size][size=1][color=#0000ff]As[/color][/size][size=1] [/size][size=1][color=#0000ff]New[/color][/size][size=1] DropListData([/size][size=1][color=#0000ff]Me[/color][/size][size=1].DropListOptions.enmStates)

[/size][size=1][color=#0000ff]Dim[/color][/size][size=1] oMonths [/size][size=1][color=#0000ff]As[/color][/size][size=1] [/size][size=1][color=#0000ff]New[/color][/size][size=1] DropListData([/size][size=1][color=#0000ff]Me[/color][/size][size=1].DropListOptions.enmMonths)

[/size][size=1][color=#0000ff]Dim[/color][/size][size=1] oDays [/size][size=1][color=#0000ff]As[/color][/size][size=1] [/size][size=1][color=#0000ff]New[/color][/size][size=1] DropListData([/size][size=1][color=#0000ff]Me[/color][/size][size=1].DropListOptions.enmDaysOfMonth)

[/size][size=1][color=#0000ff]Dim[/color][/size][size=1] oYears [/size][size=1][color=#0000ff]As[/color][/size][size=1] [/size][size=1][color=#0000ff]New[/color][/size][size=1] DropListData([/size][size=1][color=#0000ff]Me[/color][/size][size=1].DropListOptions.enmYears)

[/size][size=1]ddlState.DataSource = oStates

ddlState.DataBind()

ddlState1.DataSource = oStates

ddlState1.DataBind()

ddlState2.DataSource = oStates

ddlState2.DataBind()

ddlDOBMonth.DataSource = oMonths

ddlDOBMonth.DataBind()

ddlDOBDay.DataSource = oDays

ddlDOBDay.DataBind()

ddlDOBYear.DataSource = oYears

ddlDOBYear.DataBind()

[/size][size=1][color=#0000ff]End[/color][/size][size=1] [/size][size=1][color=#0000ff]Sub[/color][/size]

For some reason, the .DataBind() section is throwing up an error right now. Any thoughts?

Where can I get more info on how to do this sort of thing? What would I search under? The keyword "list" returns too many irrelevant topics.

I hope my code helps others, and I look forward to getting some feedback from others.
 
First of all the arg in your constructor should be
"byVal intOption as DropListOptions"

Next, remove all that code form the constructor and create a Method/Function that returns the ArrrayList, this way you can set the returned arrayList as the DataSource of your DDL. Makes sense?
 
Get rid of the DataBind() method calls. I'll claim...err...stupidity. I do a lot of ASP.Net and you have to DataBind ASP.Net drop downs. Windows forms combos don't have a DataBind() method. They'll automatically "bind" when you set their DataSource. Pardon that - it was late...that's my excuse and I'm sticking to it! *sigh*

I'm not sure that your DropListData class is going to work as you intend. You're kind of building up your list in the class constructor and populating its internal ArrayList but I don't see that you expose that ArrayList as a property or anything - so I don't see how it's going to bind as a datasource to your combos.

While your approach *could* theoretically work if you made an accessor for your ArrayList and bound to it rather than the class itself, I'm not sure that's the approach I would take. There's nothing wrong with your approach if you can make it work. There are also a few things you could do to shorten your code - since things like lists of days, months, and years can be built using loops rather than single lines of values. Again - nothing wrong with using single lines - there are just several solutions.

I would probably make the data list class and use a seperate method (public function) for each different list (state list, year list, etc) - and each one have a return type of ArrayList. You could then make an instance of your data class and use the methods for the data sources of your combos.

For example, in your data class you might do:

Code:
Public Class DropListData

	Public Function StateList As ArrayList
		Dim oStateList As New ArrayList() 

		oStateList.Add("AZ - Arizona")
		oStateList.Add("CA - California")
		oStateList.Add("NM - New Mexico")
		'  Etc.

		Return oStateList

	End Function	
    
    Public Function MonthList As ArrayList
		Dim oMonthList As New ArrayList()
		Dim nIndex As Integer
		
		For nIndex = 1 To 12
			oMonthList.Add(MonthName(nIndex))
		Next		
		
		Return oMonthList
	End Function    
	
	Public Function DayList As ArrayList
		Dim oDayList As New ArrayList()
		Dim nIndex As Integer
		Dim sDay As String
		
		For nIndex = 1 To 31
			sDay = nIndex.ToString()
			If sDay.Length = 1 Then
				sDay = "0" + sDay
			End If
			
			oDayList.Add(sDay)
		Next nIndex
		
		Return oDayList
		
	End Function	
	
	Public Function YearList As ArrayList
		Dim oYearList As New ArrayList()
		Dim nIndex As Integer
		
		For nIndex = (DateTime.Now.Year - 100) To DateTime.Now.Year
			oYearList.Add(nIndex.ToString())
		Next		
	
		Return oYearList
	
	End Function	

End Class

I went a little overboard giving examples for each method...but that's kinda my style (from the VB6 forums). *sigh* But anyways, it does show you how you can do some of your lists a little differently.

The big stickler is the list of states. There's nothing wrong with hardcoding all of the values but in actual practice a lot of times you'd pull the state list from a text file, xml doc, database, or something. If you don't have that type of data source OR you don't want extra dependencies for your application(s) then hardcoding the list is actually a good solution. Plus, in the case of a list of US states it's not very likely that the data is going to change. ;)

To use a data list class with the methods setup differently as illustrated above in a form, you'd do something like:

Visual Basic:
Dim oDataLists As New DropListData()

ddlStates1.DataSource = oDataLists.StateList
ddlStates2.DataSource = oDataLists.StateList	
ddlYears.DataSource = oDataLists.YearList
'  Etc.

Now, if you wanted to make your existing data class work what you'd need to do is add a public property to your class that exposes your 'oData' ArrayList. I would also recommend making the Enum in the data list class Public - which will allow it to be used from your form (so you don't have to declare it twice - it kinda appears that you've declared it in the form too...)

Code:
Public ReadOnly Property DataList as ArrayList
	Get
		Return oData
	End Get		
End Property

...which you'd bind in your code like:

Code:
ddlState.DataSource = oStates.DataList

Paul

PS - I would also agree, after reading Robby's reply that hit while I was writing my epic reply, that I don't recommend doing all of your "work" in your data class constructor either. It's not really good OOP form. The only thing you should do in the constructor is setup stuff the class needs to do its thing - all other "work" should be contained in methods, subs, etc.
 
Last edited:
Paul and Bobby,

Thanks for your help. This is working now. Whereas Bobby's answer was correct and to the point, Paul's "Detailed Epic" was perfect for keeping me from having to ask any further questions.

Thanks!
 
Back
Top