We help IT Professionals succeed at work.

Loop dropdown value into label

PAI_03 asked
How would i go about looping through a dropdown list that is tied to a database table with multiple rows and columns to populate different labels. Example: When a user selects there employee ID i would like the labels to auto populate the supervisor name.
Watch Question

Vel EousResearch & Development Manager

If you attach a control changed event handler to your combobox, you can detect a new selection has been made.

From there you can extract the value of the selected item and perform a database query accordingly to return a value which can then be used to populate your label.

Take a look at the code snippet below as an example.  I used a WPF app but the same principle applies to WinForms etc.
// MainWindow.xaml
        <ComboBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="comboBox1" VerticalAlignment="Top" Width="171" IsEditable="True" ItemsSource="{Binding}"  TextBoxBase.TextChanged="comboBox1_TextChanged" IsTextSearchEnabled="False" />
        <Label Content="myLabel" Height="28" Margin="189,11,12,0" Name="label1" VerticalAlignment="Top" />

// MainWindow.xaml.cs
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace EE.WpfUI
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        public MainWindow()
            List<string> comboBoxItemsSource = new List<string>();
            this.comboBox1.DataContext = comboBoxItemsSource;

        private Dictionary<string, string> associated = new Dictionary<string, string>() { { "Hello", "World" }, { "Morning", "Glory" }, { "Good", "Night" } };

        private void comboBox1_TextChanged(object sender, TextChangedEventArgs e)
            // cast the sender which fired the event to a combobox, which is should be anyway
            ComboBox cb = sender as ComboBox;
            // retrieve the selecteditem text value
            string key = cb.SelectedItem.ToString();
            // perform a database lookup here, either altering the LINQ or replacing it with another method
            var result = (from index in associated
                          where index.Key == key
                          select index.Value).FirstOrDefault();
            // write the result to the label
            this.label1.Content = result;

Open in new window


don't think that helped me to much. Here is a copy of my code I have been working on. But the only problem is I can't figure out how to get it into a loop. The Drop down is tied to a SQL database that auto populates it.
Protected Sub ddOrg_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlOrg.SelectedIndexChanged
        If ddlOrg.SelectedValue = "PMM" Then
            ddPMM.Visible = True

        ElseIf ddlOrg.SelectedValue = "PCC" Then
            ddPMM.Visible = False

        End If

    End Sub

    Protected Sub ddPMM_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddPMM.SelectedIndexChanged

        Dim dv As DataView = CType(sqlempid.Select(DataSourceSelectArguments.Empty), DataView)

        Dim dr As DataRowView = dv.Item(0)

        lblName.Text = dr.Item("cfirstname")
        lbllastname.Text = dr.Item("clastname")
        lblsup.Text = dr.Item("csupname")

    End Sub

Open in new window

Vel EousResearch & Development Manager

If you are returning lots of data with your SQL query and not just a single value, you will need to store the returned data in a collection.

Once your returned data is in the collection you can then iterate over the collection and populate your labels etc.
figured it out through adodb