ComboBox CodeSample - Populate two ComboBoxes where contents of second depend on first

From Visual WebGui Wiki

Jump to: navigation, search


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

Personal tools