Passing id from one combobox to populate second combo box in WPF

◇◆丶佛笑我妖孽 提交于 2019-12-13 21:12:46

问题


How do I pass the categoryId for the selected CategoryName in the first combo box, which would populate the products in the second combo box /

the table structure is a below.

Table_Category CategoryId CategoryyName

Table_Product ProductId ProductName CategoryId ProductPrice ProductUnit

Thanks, Hussain Patel


回答1:


for the first combo box (cmbcategory) using ADO.NET (code), get category object (containing the categoryId and categoryName) and bind to itemsource property of first combo box (cmbcategory)

Example cmbcategory.ItemsSource = categories.DefaultView;

In the xaml code for the cmbcategory add two attributes DisplayMemberPath and SelectedValuePath. assign the the "CategoryName" and "CategoryID" to them. Example

Now you can retrieve the category id for the item selected in the cmbcategory,as below string tmp = cmbcategory.SelectedValue.ToString();

This can be passed to the ADO.NET SQL command object and products can be retrieved and the bindded to the cmbproduct combo box as below

cmbproduct.ItemsSource = products.DefaultView;

Thus for each category selected in the first cmbcategory the products are populated in the cmbproducts.

Thanks, hussain



来源:https://stackoverflow.com/questions/22495422/passing-id-from-one-combobox-to-populate-second-combo-box-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!