• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

Dropdown List with sub header based on database relationship!


This is what my data looks like:

Country              City
Canada            Toronto
Canada            Calgary
Canada          Ottawa
USA              New York
USA              Detroit
USA              Chicago

I want to display this data in a drop down list like this:

[Please select a city]
  New York

I want it grouped by country. How do I go about achieving this?

Thanks for all the comments.


  • 2
1 Solution
You won't be able to do a standard data-binding; you'll have to populate the list programmatically, since what you want in the box doesn't have a one-for-one relationship to the database list of rows. I don't do much programming for the web, but I think something like the following should work. (This assumes Country and City as string fields within the table dtTable in datasource dsDatabase; adjust as needed.) If it's not all the way, perhaps it'll at least get you pointed in a profitable direction.

dim drCurrent as DataRow
dim strLastCountry as String = ""
dim dllOutput as New DropDownList
dllOutput.Items.Add(New ListItem("[Please select a city]"))
For Each drCurrent in dsDatabase.dtTable
   If strLastCountry <> drCurrent.Country Then
      dllOutput.Items.Add(New ListItem(drCurrent.Country))
      strLastCountry = drCurrent.Country
   End If
   dllOutput.Items.Add(New ListItem("  " & drCurrent.City))
I'm not certain what I gave him will work exactly as desired, since a DropDownList is a web control and I don't do web programming. But the concept is valid, and would either solve the problem or at least point in the right direction, IMHO.  If someone else knows this is out in left field, then delete it; otherwise, I think it should be PAQed with points to me.

The questioner used to be pretty active here, but seems to have disappeared in the last month.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now