• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 336
  • 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.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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