Search code examples
vb.netcomboboxcascadingdropdown

VB.net Cascading ComboBoxes Connected to DataSet from Access - Changing Units, & Decimal to Fractions


I've got a fairly complex ComboBox scenario, and being new to programming I'm struggling with what the best approach to take is.

I have a DataSet with a DataTable that has several numeric columns of Data. The numeric data is composed of distances given in U.S. Standard units. I currently have my ComboBoxes set up and working, but I need to expand on what I currently have in two ways.

  1. I need to be able to convert the Decimal numbers in my data column being displayed to Fractions, is there a way to do this and maintain databinding? In this case its the Display Member of the data source...

  2. I need to be able to display my drop down options in different sets of units... I've written Unit Conversion classes to help take care of this, but I don't know if I can somehow do this as well and maintain databinding? I'd like to convert the units on the display members as well...

    Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As         System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
     Select Case ComboBox1.SelectedItem
        Case "Tire1"
            With ComboBox2
                .DataSource = Tire1BindingSource
                .ValueMember = "OD"
                .DisplayMember = "OD"
            End With
        Case "Tire2"
            With ComboBox2
                .DataSource = Tire2BindingSource
                .ValueMember = "OD"
                .DisplayMember = "OD"
            End With
        Case "Tire3"
            With ComboBox2
                .DataSource = Tire3BindingSource
                .ValueMember = "OD"
                .DisplayMember = "OD"
            End With
        Case "HubCap"
            ComboBox3.DataSource = Nothing
            With ComboBox2
                .DataSource = HubcapBindingSource
                .ValueMember = "ID"
                .DisplayMember = "ID"
            End With
      End Select
    End Sub
    
    Private Sub ComboBox2_SelectedIndexChanged(sender As System.Object, e As      System.EventArgs) Handles ComboBox2.SelectionChangeCommitted
    Select Case ComboBox1.SelectedItem
        Case "Tire1"
            With ComboBox3
                .DataSource = Tire1BindingSource
                .ValueMember = "ID"
                .DisplayMember = "Weight"
            End With
        Case "Tire2"
            With ComboBox3
                .DataSource = Tire2BindingSource
                .ValueMember = "ID"
                .DisplayMember = "Weight"
            End With
        Case "Tire3"
            With ComboBox3
                .DataSource = Tire3BindingSource
                .ValueMember = "ID"
                .DisplayMember = "Weight"
            End With
        Case "HubCap"
            ComboBox3.DataSource = Nothing
            With ComboBox2
                .DataSource = HubCapBindingSource
                .ValueMember = "ID"
                .DisplayMember = "ID"
            End With
    End Select
    

What is the best approach for using ComboBoxes when dealing with issues such as displaying fractions and units...


Solution

  • Yes you can draw fractional without affecting DataBindings. You have to set DrawMode property and handle DrawItem and MeasureItem events. For more information read this MSDN article;