Dropdown List with sub header based on database relationship!

Posted on 2006-05-17
Last Modified: 2006-11-18

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.


Question by:rajesh75
    LVL 20

    Accepted 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))
    LVL 20

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now