Reputation: 15
Could you please tell me how to solve this problem?
I want to autoselect combobox with value form database for display (not select with text).
Add the value and text to ComboBox
public void ComBoList(JArray Data)
{
comboBoxBrand.Items.Clear();
comboBoxSize.Items.Clear();
comboBoxPCBrand.Items.Clear();
comboBoxProductOf.Items.Clear();
comboBoxBrand.DisplayMember = "Text";
comboBoxBrand.ValueMember = "Value";
comboBoxSize.DisplayMember = "Text";
comboBoxSize.ValueMember = "Value";
comboBoxPCBrand.DisplayMember = "Text";
comboBoxPCBrand.ValueMember = "Value";
comboBoxProductOf.DisplayMember = "Text";
comboBoxProductOf.ValueMember = "Value";
JArray AllData = JArray.Parse(Data.ToString());
int JCount = AllData.Count;
for (int i = 0; i <= (JCount - 1); i++)
{
foreach (var Values in AllData[i])
{
if (i == 0)
{
int BId = int.Parse(Values["HddBrandId"].ToString());
comboBoxBrand.Items.Add(new { Text = Values["BrandName"].ToString(), Value = BId });
}
else if (i == 1)
{
int SId = int.Parse(Values["SizeId"].ToString());
comboBoxSize.Items.Add(new { Text = Values["Size"].ToString(), Value = SId });
}
else if (i == 2)
{
int PId = int.Parse(Values["PCBrandId"].ToString());
comboBoxPCBrand.Items.Add(new { Text = Values["BrandName"].ToString(), Value = PId });
}
else if (i == 3)
{
int CId = int.Parse(Values["CountryId"].ToString());
comboBoxProductOf.Items.Add(new { Text = Values["CountryName"].ToString(), Value = CId });
}
}
}
}
Select combobox with value from database:
comboBoxBrand.SelectedValue = Data.BrandId; // int Data from Database
comboBoxSize.SelectedValue = Data.SizeId; // int Data from Database
comboBoxPCBrand.SelectedValue = Data.PCBrandId; // int Data from Database
comboBoxProductOf.SelectedValue = Data.ProductOfId; // int Data from Database
Please help me.
Thank you.
Upvotes: 0
Views: 205
Reputation: 74605
DisplayMember and ValueMember are used when a combobox is populated using databinding (by setting the .DataSource
property to a bindable collection)
Instead of putting things in your combo .Items
collection, put them in a List, set the .DataSource
to the list and then when you set .SelectedValue
it will select an item from the list if the value you set equals the value of one of the .Value
properties of the list items
To this end it's probably easier to use something like a ValueTuple (or a full class) than an anonymous type, but an anonymous type could be bound, you'd just have to have a fiddle to create a list for it, perhaps
var listToBind = new int []{}.Select(s=> new{ Text = "", Val=0}).ToList();
This would give you a List<anonymoustype<string,int>>
into which you can load your anonymous typed items.. you can then set the combo DataSource to it and things should work as you expect.. though getting them out via SelectedItem will return you an object wrapping around the anonymous type, which you'll struggle to work with (my vote; make a proper class for DisplayValue pairs, or use a tuple, or something that already exists like KeyValuePair<T,T2>
Upvotes: 2