根据之前在 XML 中选择的组合框填充组合框 [英] populate a combobox based on previous combobox selection in XML

查看:18
本文介绍了根据之前在 XML 中选择的组合框填充组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 .net 初学者.我正在阅读 XML 文件 并将其显示在两个组合框中,即 cbProduct 和 <代码>cbBrandName

I am a .net beginner. I am reading a XML file and showing it in two comboboxes ie., cbProduct and cbBrandName

我需要在 cbBrandName 中显示与 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中的所有文本值.如何使其仅显示链接到 cbProduct 中选定的 xml 文件productname"列的文本值.

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 的回答中使用了其中的两个部分,我将尝试对其进行解释.第一个是 .Select(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 的另一部分是 .Where(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"); 

会给你一个 {"a"} 的列表.用 x != "b" 替换 x == "a" 会给你一个 {"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 语句拆分成这样,将它们放在单独的行上,然后我可以阅读这些行:我得到一个列表,WHERE 这是真的,然后选择其他基于在它上面,然后把它变成一个数组."

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆