填充根据XML以前的组合框中选择一个组合框 [英] populate a combobox based on previous combobox selection in XML
问题描述
我是一个.NET初学者。我两个组合框即, cbProduct $阅读 XML文件,并将其显示C $ C>和
cbBrandName
I am a .net beginner. I am reading a XML file and showing it in two comboboxes ie., cbProduct
and cbBrandName
我要显示的文字 cbBrandName $ 。C $ C>相对于所选文本
cbProduct
I need to show text in cbBrandName
with respect to the selected text in cbProduct
.
我实现下面的代码:
DataSet ds = new DataSet();
ds.ReadXml(@"..\..\stock.xml");
cbProduct.DataSource = ds.Tables[0].DefaultView.ToTable(true, "productname");
cbProduct.DisplayMember = "productname";
cbBrandName.DataSource = ds.Tables[0].DefaultView.ToTable(true, "brandname");
cbBrandName.DisplayMember = "brandname";
上面的代码是显示所有的文字值 cbBrandName
。如何使它只显示其链接到XML文件中所选择的产品名称列 cbProduct
文本值。
The above code is showing all the text values in cbBrandName
. How to make it to show only the text values which are linked to the selected "productname" column of xml file in cbProduct
.
请帮助。
结果在此先感谢。
Please Help.
Thanks in Advance.
推荐答案
LINQ看起来更吓人比它。有一个在Anirudha的回答正在使用的这两位,我会试着解释。首先是。选择(X =>
这意味着对于列表中的每一件事情,用什么替代它x代表列表中的每个项目
LINQ looks much more scary than it is. There's two bits of it being used in Anirudha's answer, which I'll try to explain. The first is .Select(x=>
. This means "For each thing in the list, replace it with something". The x represents each item in the list.
例如:
new string[]{"a", "b", "c"}.Select(x=>x.ToUpper());
原来的{A,b,C},一个数组{A,b,C}组成的数组。它只是说把每个东西的清单,通过调用 ToUpper的()
它不管你得到更换。
turns an array of {"a", "b", "c"}, into an array of {"A", "B", "C"}. It's just saying "Take the each thing in the list, and replace it with whatever you get by calling ToUpper()
on it.
LINQ的另一位为。凡(X =方式>
这只是说:给我一个较小的列表,其中只有东西的地方这句话是真的因此,
The other bit of LINQ is .Where(x=>
. That just says "Give me a smaller list which only has things where this statement is true". So
new string[]{"a", "b", "c"}.Where(x=>x == "a");
会给你{一}清单。更换 X ==A
与 X!=b
会给你的{A,C}清单。因此,在代码的第二位,你说:我做那件事,我与它的产品名称替换每个项目之前,我想筛选出任何不符合我想匹配的内容。然后,我改变什么就走了。
will give you a list of {"a"}. Replacing x == "a"
with x != "b"
will give you a list of {"a", "c"}. So in the second bit of code, you're saying "before I do that thing where I replace each item with its productname, I want to filter out anything that doesn't match what I want to match. Then I transform what's left."
要运用这些代码示例中,我将重新格式化行和评论他们
To apply these to the code example, I'll reformat the lines and comment them.
// To set the first combo box:
cbProduct.Items.AddRange( // Add everything we produce from this to the cbProduct list
doc.Descendants("items") // For each thing that represents an "items" tag and it's subtags
.Select(x=>x.Element("productname").Value) // Transform it by getting the "productname" element and reading it's Value.
.ToArray<string>()); // Then convert that into a string[].
// To set the second combo box:
string product2Search=cbProduct.SelectedItem.ToString();// get the currently selected value of cbProduct.
cbBrandName.Items.Clear(); //clears all items in cbBrandNamedoc
cbBrandName.Items.AddRange( // Add everything we produce from this to the cbBrandName list
doc.Descendants("items") // For each thing that represents an "items" tag and it's subtags
.Where(x=>x.Element("productname").Value==product2Search) // Filter our list to only those things where the productname matches what's currently selected in cbProduct (which we just stored)
.Select(y=>y.Element("brandname").Value) // Transform it by getting the "brandname" element and reading it's Value.
.ToArray<string>()); // Then convert that into a string[]
是有帮助吗?当我自己的编码,我喜欢把它们放在单独的行这样的分割长LINQ陈述了起来,然后我可以读下来的台词:我得到一个列表,这是真的,那么选择基于这另一件事它,把它变成一个数组。
Is that helpful? When I'm coding myself, I like to split long LINQ statements up by putting them on separate lines like this, and then I can just read down the lines: "I get a list WHERE this is true, then SELECT this other thing based on it, and turn it into an ARRAY."
这篇关于填充根据XML以前的组合框中选择一个组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!