I am a .net beginner. I am reading a XML file and showing it in two comboboxes ie., cbProduct and cbBrandName
I need to show text in cbBrandName with respect to the selected text in cbProduct.
I implemented the below code:
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";
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 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.For example:
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.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”. Sowill give you a list of {“a”}. Replacing
x == "a"withx != "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.
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.”