Using SelectedItem property of ComboBox w/Linq Anonymous Type?

How about converting it to a list, then choosing the correct one from it. Since SelectedItem doesn't seem to work, you may want to try setting SelectedValue var productList = (from p in db. Products select new { ProductId = p.

ProductID, Name = p. Name }).ToList(); comboBox1. DataSource = productList; comboBox1.

DisplayMember = "Name"; comboBox1. ValueMember = "ProductId"; comboBox1. SelectedValue = 5.

How about converting it to a list, then choosing the correct one from it. Since SelectedItem doesn't seem to work, you may want to try setting SelectedValue. Var productList = (from p in db.

Products select new { ProductId = p. ProductID, Name = p. Name }).ToList(); comboBox1.

DataSource = productList; comboBox1. DisplayMember = "Name"; comboBox1. ValueMember = "ProductId"; comboBox1.

SelectedValue = 5.

I just tried it, it doesn't seem to change the selected item. It also only works if done right when it's loaded. The selected item may get set in another function.Thanks.

– Ryan Roper Feb 9 '09 at 23:49 Did you try setting SelectedValue to the correct ProductId instead. I'll update the code. – tvanfosson Feb 9 '09 at 23:56 Also, I had an error in the selection logic.

I've fixed that -- should have been a Where method instead of a Select method. Changing that may make the previous example work. – tvanfosson Feb 9 '09 at 23:59 Actually this caused me to stumble on the answer.

Apparently once you set the valuemember property to the name of the field you want in the anon type just setting the SelectedValue = 5 changes the selected item. – Ryan Roper Feb 10 '09 at 0:11 Sure. I don't know why I was thinking you had to set the name.

– tvanfosson Feb 10 '09 at 1:02.

This should do the trick var list = (from p in db. Products select new { p. ProductId, p.Name }).ToList(); comboBox1.

DataSource = list; comboBox1. SelectedItem = list0.

That would only add the first result from the LinQ query to the dropdown list. I want to add all the results to the list, but select a specific item afterword. – Ryan Roper Feb 9 '09 at 23:18 I see what you're saying, give me a sec and i'll update the answer – JaredPar Feb 9 '09 at 23:39 That works if you know which record in the list is the one you want by record number.It doesn't work if you set the selecteditem in a different function from the one that loads the list.

Thanks again. – Ryan Roper Feb 9 '09 at 23:58.

In C# 3.5 using a ComboBox to display the results of a LinQ Query. How do I set the selecteditem property of the combobox when the LinQ query is returning an anonymous type? The anonymous type I'm actually using is more complicated then that but it suffices for explanation.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions