Bind ComboBox with selected value from other using C# and VB.Net in Windows Application

Last Reply 6 months ago By dharmendr

Posted 6 months ago

There are four combobox. first three combobox it is binded with many units.

If first combox selects value as gm, Second combobox selects value as kg, Third combobox as litre.

Fourth combobox have binded with these three values in Windows application.

I have one datasource for 3 combobox

Now for fourth one I just need the three values.

Posted 6 months ago

Hi gokuldas,

Check this example. Now please take its reference and correct your code.

C#

static DataTable dtSelectedUnits;
private void Form1_Load(object sender, EventArgs e)
{
    BindComboBox(GetData(), cmbUnits1, "Text", "Value");
    BindComboBox(GetData(), cmbUnits2, "Text", "Value");
    BindComboBox(GetData(), cmbUnits3, "Text", "Value");
}

private void BindComboBox(DataTable dt, ComboBox cmb, string displayMember, string valueMember)
{
    cmb.DataSource = dt;
    cmb.DisplayMember = displayMember;
    cmb.ValueMember = valueMember;
}

private static DataTable GetData()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] { 
                    new DataColumn("Text", typeof(string)),
                    new DataColumn("Value", typeof(string)) });
    dt.Rows.Add("Select", "0");
    dt.Rows.Add("kg", "kg");
    dt.Rows.Add("gm", "gm");
    dt.Rows.Add("liter", "liter");
    return dt;
}

private void cmbUnits1_SelectionChangeCommitted(object sender, EventArgs e)
{
    dtSelectedUnits = new DataTable();
    dtSelectedUnits.Columns.AddRange(new DataColumn[] { 
                    new DataColumn("Text", typeof(string)),
                    new DataColumn("Value", typeof(string)) });
    dtSelectedUnits.Rows.Add("Select", "0");
    dtSelectedUnits.Rows.Add(cmbUnits1.SelectedValue, cmbUnits1.SelectedValue);
}

private void cmbUnits2_SelectionChangeCommitted(object sender, EventArgs e)
{
    dtSelectedUnits.Rows.Add(cmbUnits2.SelectedValue, cmbUnits2.SelectedValue);
}

private void cmbUnits3_SelectionChangeCommitted(object sender, EventArgs e)
{
    dtSelectedUnits.Rows.Add(cmbUnits3.SelectedValue, cmbUnits3.SelectedValue);
    BindComboBox(dtSelectedUnits, cmbUnits4, "Text", "Value");
}

VB.Net

Shared dtSelectedUnits As DataTable
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    BindComboBox(GetData(), cmbUnits1, "Text", "Value")
    BindComboBox(GetData(), cmbUnits2, "Text", "Value")
    BindComboBox(GetData(), cmbUnits3, "Text", "Value")
End Sub

Private Sub BindComboBox(ByVal dt As DataTable, ByVal cmb As ComboBox, ByVal displayMember As String, ByVal valueMember As String)
    cmb.DataSource = dt
    cmb.DisplayMember = displayMember
    cmb.ValueMember = valueMember
End Sub

Private Shared Function GetData() As DataTable
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn() {New DataColumn("Text", GetType(String)), New DataColumn("Value", GetType(String))})
    dt.Rows.Add("Select", "0")
    dt.Rows.Add("kg", "kg")
    dt.Rows.Add("gm", "gm")
    dt.Rows.Add("liter", "liter")
    Return dt
End Function

Private Sub cmbUnits1_SelectionChangeCommitted(ByVal sender As Object, ByVal e As EventArgs) Handles cmbUnits1.SelectionChangeCommitted
    dtSelectedUnits = New DataTable()
    dtSelectedUnits.Columns.AddRange(New DataColumn() {New DataColumn("Text", GetType(String)), New DataColumn("Value", GetType(String))})
    dtSelectedUnits.Rows.Add("Select", "0")
    dtSelectedUnits.Rows.Add(cmbUnits1.SelectedValue, cmbUnits1.SelectedValue)
End Sub

Private Sub cmbUnits2_SelectionChangeCommitted(ByVal sender As Object, ByVal e As EventArgs) Handles cmbUnits2.SelectionChangeCommitted
    dtSelectedUnits.Rows.Add(cmbUnits2.SelectedValue, cmbUnits2.SelectedValue)
End Sub

Private Sub cmbUnits3_SelectionChangeCommitted(ByVal sender As Object, ByVal e As EventArgs) Handles cmbUnits3.SelectionChangeCommitted
    dtSelectedUnits.Rows.Add(cmbUnits3.SelectedValue, cmbUnits3.SelectedValue)
    BindComboBox(dtSelectedUnits, cmbUnits4, "Text", "Value")
End Sub

Screenshot