Search KB Filter article types
ComboBox CodeSample - Populate two ComboBoxes where contents of second depend on first
Categories: Selection, Built-in
Tags: Developers, Data Binding, 1. Beginner, 2. Intermediate, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
Revision: 1
Posted: 26/Sep/2009
Updated: 23/July/2010
Status: Publish
Types: Code

This article will have a few sections added to it soon, based on the following article type skeleton: CodeSample
Overview

This code sample shows how you can populate two ComboBoxes and have the contents of one depend on the other.

This sample uses DataDable and DataView objects for help, but this can of course be done what ever way you choose

VB.NET Code
Imports Gizmox.WebGUI.Forms
 
Public Class TwoComboboxes
 
    Dim ValueTable As DataTable
    Dim ValueView As DataView
    Dim CountryTable As DataTable
 
    Friend WithEvents cboCountry As Gizmox.WebGUI.Forms.ComboBox
    Friend WithEvents cboCity As Gizmox.WebGUI.Forms.ComboBox
 
    Sub New()
 
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
 
        ' Add any initialization after the InitializeComponent() call.
        CreateComboboxes()
    End Sub
    Private Sub TwoComboboxes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ValueTable = New DataTable
        ValueTable.Columns.Add("Country", GetType(String))
        ValueTable.Columns.Add("City", GetType(String))
 
        CountryTable = New DataTable
        CountryTable.Columns.Add("Country")
 
        PopulateTables()
 
        cboCity.DataSource = ValueView
        cboCity.ValueMember = "City"
        cboCity.DisplayMember = "City"
 
        cboCountry.DataSource = CountryTable
        cboCountry.DisplayMember = "Country"
        cboCountry.ValueMember = "Country"
 
    End Sub
 
    Private Sub CreateComboboxes()
        '
        'cboCountry
        '
        Me.cboCountry.Location = New System.Drawing.Point(12, 38)
        Me.cboCountry.Name = "cboCountry"
        Me.cboCountry.Size = New System.Drawing.Size(251, 21)
        Me.cboCountry.TabIndex = 0
        Me.cboCountry.Text = "ComboBox1"
        '
        'cboCity
        '
        Me.cboCity.Location = New System.Drawing.Point(312, 38)
        Me.cboCity.Name = "cboCity"
        Me.cboCity.Size = New System.Drawing.Size(307, 21)
        Me.cboCity.TabIndex = 1
        Me.cboCity.Text = "ComboBox2"
 
        Me.Controls.Add(Me.cboCity)
        Me.Controls.Add(Me.cboCountry)
 
    End Sub
    Private Sub PopulateTables()
        CountryTable.Rows.Add("Iceland")
        CountryTable.Rows.Add("UK")
        CountryTable.Rows.Add("Israel")
 
        ValueTable.Rows.Add("Iceland", "Reykjavik")
        ValueTable.Rows.Add("Iceland", "Akureyri")
        ValueTable.Rows.Add("Iceland", "Husavik")
        ValueTable.Rows.Add("Iceland", "Kopasker")
 
        ValueTable.Rows.Add("UK", "London")
        ValueTable.Rows.Add("UK", "Birmingham")
        ValueTable.Rows.Add("UK", "Manchester")
 
        ValueTable.Rows.Add("Israel", "Kfar Saba")
        ValueTable.Rows.Add("Israel", "Tel Aviv")
        ValueTable.Rows.Add("Israel", "Jerusalem")
 
        ValueView = New DataView(ValueTable)
    End Sub
 
    Private Sub cboCountry_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCountry.SelectedIndexChanged
        If cboCountry.SelectedItem Is Nothing Then
            ValueView.RowFilter = "1 = 0"
        Else
            Dim dr As DataRow = cboCountry.SelectedItem.Row()
            ValueView.RowFilter = "Country = '" + dr("Country") + "'"
            cboCity.Update()
        End If
    End Sub
End Class

C# Code
The conversion of this code to C# has not been completed yet.

About the author

Related Articles

Selection  
Title Update Author
Tags: Developers, Data Binding, Navigation, 1. Beginner, 2. Intermediate, Data Binding, Navigation, ASP.NET, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
11/Jan/2009    2009/01/11
Control of a combobox with a checked list and a tree.
Tags: Developers, Navigation, 2. Intermediate, 3. Advanced, Customization, Layouting, Navigation, Pre v6.3, v6.3, v6.4 and Later
06/Jan/2008    2008/01/06
This code demonstrates the usage of the different AutoCompleteMode values property of ComboBox control
Tags: Developers, Data Binding, Navigation, 1. Beginner, 2. Intermediate, 3. Advanced, Navigation, Pre v6.3, v6.3, v6.4 and Later
12/Jan/2007    2007/01/12
Tags: Developers, Data Binding, Windows & Dialogs, 1. Beginner, 2. Intermediate, Customization, Data Binding, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
03/Jan/2009    2009/01/03
Tags: Architects, Developers, 1. Beginner, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
04/Dec/2010    2010/12/04
Tags: Developers, 1. Beginner, 2. Intermediate, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
21/July/2010    2010/07/21
.NET HTML5 Web, Cloud and Mobile application delivery | Sitemap | Terms of Use | Privacy Statement | Copyright © 2005-2012 Visual WebGui®       Visual WebGui weblog on ASP.NET Gizmox Blog Visual WebGui Group on LinkedIn Visual WebGui updates on Twitter Visual WebGui Page on Facebook Visual WebGui YouTube Channel Visual WebGui Platform News RSS