如何将数据从数据库绑定到另一个下拉列表中选择的索引更改的下拉列表? [英] how to bind data from database to dropdownlist on another dropdownlist's selected index change?
问题描述
这是关于c#windows app
我在绑定数据时遇到问题..
这就像我要表喜欢
brandTbl(id(pk),brandName,Description)和另一个
typeTbl(id(pk),brandId(fk),类型,描述)
i将dopdown1与品牌ValueMember = id绑定,DisplayMember = brandName
现在
i想要使用Dropdown2绑定数据,类型为DisplayMember = type,ValueMember = id
选择下拉列表的索引更改1 ..
And我不知道该怎么做..
可以anyOne帮我解决这个问题???
这是一个解决方案假设您一次加载所有数据,只要没有太多项目就可以正常工作 - 这是不应该的,因为否则让用户选择ComboBoxes是没有用的。
它的工作原理是设置RowFilter-Property当您选择父-ComboBox的新条目时,DataView是子-ComboBox的DataSource。
表单启动中的某处(在构造函数或OnLoad(..) - 或OnShown(..) - 覆盖):
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;
作为表格中的一种方法:
< pre lang =cs> private void BrandComboBoxSelectedIndexChanged( object sender,EventArgs e)
{
if (brandComboBox.ValueMember == id)
((DataView)typeComboBox.DataSource)。RowFilter = brandId = + brandComboBox.SelectedValue;
}
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???
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):
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:
private void BrandComboBoxSelectedIndexChanged(object sender, EventArgs e) { if (brandComboBox.ValueMember == "id") ((DataView)typeComboBox.DataSource).RowFilter = "brandId = " + brandComboBox.SelectedValue; }
这篇关于如何将数据从数据库绑定到另一个下拉列表中选择的索引更改的下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!