Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
It's about c# windows app

i'm having problem on binding data..
the thing is like i have to table like
brandTbl(id(pk),brandName,Description) and Another
typeTbl(id(pk),brandId(fk),type,Description)

i bind the dopdown1 with brand ValueMember=id,DisplayMember=brandName

and now
i wants to bind the data with Dropdown2 with type with DisplayMember=type,ValueMember=id
On selected index change of dropdown1..

And i don't know how to do that..
can anyOne help me out with this???
Posted
Updated 20-May-15 11:36am
v3

1 solution

This is a solution which assumes that you load all data at once, which will work fine as long as there aren't too many items - which there shouldn't be because otherwise it wouldn't be useful to let the user select with ComboBoxes.

It works by setting the RowFilter-Property of the DataView that is the DataSource for the "child"-ComboBox when you select a new entry of the "parent"-ComboBox.

Somewhere in your Form-startup (in the constructor or OnLoad(..)- or OnShown(..)-overrides):
C#
brandComboBox.DataSource = brandTbl.DefaultView;
brandComboBox.ValueMember = "id";
brandComboBox.DisplayMember = "brandName";

typeComboBox.DataSource = typeTbl.DefaultView;
typeComboBox.ValueMember = "id";
typeComboBox.DisplayMember = "type";

BrandComboBoxSelectedIndexChanged(null, null);
brandComboBox.SelectedIndexChanged += BrandComboBoxSelectedIndexChanged;


As a method in your form:
C#
private void BrandComboBoxSelectedIndexChanged(object sender, EventArgs e)
{
    if (brandComboBox.ValueMember == "id")
        ((DataView)typeComboBox.DataSource).RowFilter = "brandId = " + brandComboBox.SelectedValue;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900