I have a problem that I'm struggling with.
I fill a combobox this way:
RDTA_cb_provincias.DataSource = provincias;
RDTA_cb_provincias.DisplayMember = IdiomaBD.ObjCI.ToString().Equals("eu-ES")
? "TTEXNOMBRPROV_EU"
: "TTEXNOMBRPROV_ES";
RDTA_cb_provincias.ValueMember = "TCODIDTERRITORIO";
There's some stuff depending on the language of the user, but i guess that's not important here.
Well, when i want to select depending on the value member i do this:
RDTA_cb_provincias.SelectedValue = provincia2.TCODIDTERRITORIO;
But for some reason it won't show up the actual item I want to show. While debugging i can see that RDTA_cb_provincias.SelectedValue matches provincia2.TCODIDTERRITORIO.
For example, I've set SelectedValue to 51, but when i see the SelectedItem inside the ComboBox once I've changed the value, It shows up an object that has 47 as its TCODIDTERRITORIO.
What can it be? Or how can I bypass this problem (Maybe iterating the whole ComboBox item list until i find the one i want to select?)
Thanks in advance!
You should use SelectedItem instead:
RDTA_cb_provincias.SelectedItem = provincia2;
Or you can use SelectedIndex as follows:
RDTA_cb_provincias.SelectedIndex = RDTA_cb_provincias.FindStringExact(provincia2.TCODIDTERRITORIO)
Related
The program is a minidatabase-ish thing for product ordering purposes, where I keep the unique orders in a struct, and the productNames in an enum (for practice basically). I only have 3 productnames (Product0, Product1, Product2), and they are added to a combobox (cbo_productNameEdit.DataSource = Enum.GetNames(typeof(productNames));).
Anyway, after saving an order, I want this combobox to change it's selected item to the saved product's name, but it fails to do so. I checked it with a MessageBox, to see if it didn't store it properly...
MessageBox.Show(Orders[cbo_productID.SelectedIndex].productName.ToString());
cbo_productNameEdit.SelectedItem = Orders[cbo_productID.SelectedIndex].productName;
... the messagebox returned Product2, which is indeed the correct one, but the selected item stayed at Product0.
One thing you could do to solve it is to set the SelectedIndex instead of SelectedItem property on the combobox. By default Enums are 0 based integers, so the index will corespond to the value of the enum.
cbo_productNameEdit.SelectedIndex = (int)Enum.Parse(typeof(productNames),
Orders[cbo_productID.SelectedIndex].productName.ToString());
Because you used .DataSource property for filling ComboBox with items
You need to use .SelectedValue for setting selecting item
cbo_productNameEdit.SelectedValue = Orders[cbo_productID.SelectedIndex].productName;
From MSDN: ComboBox.SelectedValue
There is a combobox which i fill with this loop
foreach (Machine.Types machine in machineList)
{
cbMachineGUI.Items.Add(machine);
}
after that i want the selected index to be one specific machine.
string machineComboBox = SettingsManager.getParameter("MachineType");
cbMachineGUI.SelectedItem = machineComboBox;
The parameter is correct and set, but the selecteditem of the combobox is always nothing.
if i set the machines in the properties of the combobox (not via the loop) it works. but i need the combobox to be variable.
any suggestions?
The problem is that what you put in Items and what you set SelectedItem to are different types.
You are filling the Items collection with Machine.Types instances, and setting SelectedItem to a string instance.
Using IndexOf like other answers suggest will not help, as this will not do anything that setting SelectedItem does not already do. It still won't find machineComboBox in the Items collection, just like it can't find it now.
You need to use matching types, so do one of these things (depending on how else you use the values in the combobox):
Convert Machine.Types to a string when filling the collection:
cbMachineGUI.Items.Add(machine.ToString());
Convert machineComboBox into an instance of Machine.Types that will match the one in Items when setting SelectedItem - how to do it depends on what Machine.Types is
Find the correct item yourself when setting SelectedItem:
cbMachineGUI.SelectedItem = cbMachineGUI.Items
.OfType<Machine.Types>()
.FirstOrDefault(item => item.ToString() == machineComboBox);
Either way, you must make a conversion between these two types somewhere.
Instead of setting SelectedItem, I suggest you find the item's index and set the selected index.
Something like this:
string machineComboBox = SettingsManager.getParameter("MachineType");
int itemIndex = cbMachineGUI.Items.IndexOf(machineComboBox);
cbMachineGUI.SelectedIndex = itemIndex;
You could try the following:
cbMachineGUI.SelectedIndex = cbMachineGUI.Items.IndexOf("MachineType"); // or whatever you want to select
It could be possible that the item you are trying to set doesn't present in combobox item list and since you haven't actually selected anything it sets to nothing. To check if the item does exist do below
string machineComboBox = SettingsManager.getParameter("MachineType");
if(cbMachineGUI.Items.IndexOf(machineComboBox) >= 0)
cbMachineGUI.SelectedItem = machineComboBox;
Quoting from MSDN documentation:
When you try to set the SelectedItem property to an object, the
ComboBox attempts to make that object the currently selected one in
the list. If the object is found in the list, it is displayed in the
edit portion of the ComboBox and the SelectedIndex property is set to
the corresponding index. If the object does not exist in the list, the
SelectedIndex property is left at its current value.The ComboBox class
searches for the specified object by using the IndexOf method.
Check ComboBox.SelectedItem for more information.
I am getting first time this kind of problem
I am setting value of dropdownlist at page load from dataset
But this is auto setting 0 index .....
My code :
ddlEbitda.SelectedValue = objDataset.Tables[0].Rows[0]["acq_ebitda"].ToString();
I checked at immediate windows ...
objDataset.Tables[0].Rows[0]["acq_ebitda"].ToString();
"Between 40-60 %" ( come value from data set is right)
ddlEbitda.SelectedValue
"Up to 10 Million Dollar " ( this is default value setting )
I tried many different code to solve this problem :
ddlEbitda.ClearSelection();
string Ebitda = objDataset.Tables[0].Rows[0]["acq_ebitda"].ToString();
ddlEbitda.Items.FindByValue(Ebitda).Selected = true;
string Ebitda = objDataset.Tables[0].Rows[0]["acq_ebitda"].ToString();
ddlEbitda.SelectedIndex = ddlEbitda.Items.IndexOf(ddlEbitda.Items.FindByValue(Ebitda));
But still not able to solve this problem ....truth is I am not able to understand what is problem .....
This should work
string value=objDataset.Tables[0].Rows[0]["acq_ebitda"].ToString();
ddlEbitda.Items.FindByValue(value).Selected = true;
But make sure when you are binding the dropdown with DataSource, at that time acq_ebitda should be DataValueField.
If it is DataTextField then you should try something like this
ddlEbitda.Items.FindByText(value).Selected = true;
Ensure that the DropDownList is populated/bound inside this code block in the Page_Load method
If (!IsPostBack)
{
}
and immediately after set the selected item.
ddlEbitda.SelectedValue
Make sure whether your dropdown list is correctly bound to the dataset and data is populated . specially make sure the value you are assigning is in the item list. you can easily check with quick watch in VS so you can make sure the value you are assigning is inside the dropdown items. because the way you are doing is OK
I have the code below
FooCB.DisplayMember = "FooNome";
FooCB.ValueMember = "Foo";
FooCB.DataSource = FooRepository.Instance.All();
FooCB.DataBindings.Add("SelectedItem", Bar, "Foo");
but when I display the form the SelectedItem is always the first.
What am I doing wrong?
I have been struggling a little with the behaviour of Winforms comboboxes and databinding recently and these are my observations (.Net4) when binding the ComboBox.DataSource to a list of items and also binding an object property to ComboBox.SelectedItem.
When binding a list of objects (in your case List<Foo>) to ComboBox.DataSource, the first object in the list is always shown in the combobox.
If you bind an object property to ComboBox.SelectedItem (in your case Bar.Foo) and that object property matches one of the combobox list objects then that object is displayed in the combobox. If the object property is null (Bar.Foo == null) or the object property is not in the combobox list then the first object is shown in the combobox.
Setting ComboBox.SelectedItem = null or ComboBox.SelectedIndex = -1 clears the displayed item on the combobox even though this seems to warn against it. And will set your bound object property to null.
If a user clears the combobox selection when using ComboBox.DropDownStyle == DropDown (with backspace) then the bound object property is set to null.
If you have implemented INotifyPropertyChanged on the object whose property is bound to Combobox.SelectedItem (Bar.Foo) and you programatically set the bound property to a value and that value appears in the combobox list then the changed value will be displayed. If you set the property to null or a value not in the list then the combobox displayed value will not change.
So what can you do about it? The only real issue I have is having no value displayed when my bound property is null so I have just been explicitly setting Combobox.SelectedItem = null as in point #3. You may be able to extend ComboBox and override the default behaviour but so far I have been content with an extra line of code here and there combined with using default values on non-nullable properties.
Probably you are missing some decleration. If you created the Combobox from the Tool Box, -I had the similar problem- you might want to add name of the Combobox's Name as a tag on XAML.
Other than that, if you created it dynamically by code, check if you are missing any decleration for class.
I can't tell from the OP's code whether I'm answering their question, but maybe this will help somebody reading this question. The ComboBox has four ways to set the current value:
SelectedIndex
SelectedItem
SelectedText
SelectedValue
You need to be consistent about what you're setting (and about which event handler you're using). You'll get an error if you set SelectedIndex to something dumb (less than -1 or longer than the list). However, you don't get errors setting the other three to something that doesn't exist for that type of selection.
Suppose you use a Dictionary (that's pseudo code) as a binding source and set DisplayMember = "Value" and ValueMember = "Key", the mapping would look like:
SelectedIndex - -1 to index of last item
SelectedItem - KeyValuePair<Key, Value>
SelectedText - Dictionary value
SelectedValue - Dictionary key
Supplying either value or key to SelectedItem will not generate an error, it will simply act like the OP has described. And that's why I thought this answer might help somebody.
I might also note that, if you're swapping out the contents of the ComboBox, it's not always safe to use SelectedIndex. Suppose the same basic kind of data is in the ComboBox, but selections are limited in some cases compared to others. Using SelectedIndex to persist a previous selection that was still valid in the new list of options is only going work if that previous selection held the exact same place in the list. You'd almost think this was the voice of very, very recent experience speaking...
I am using the AutoCompleteBox in WPF, I populate the suggestions with a List that consists of four fields. When the user selects an item and I reach my eventHandler, i can see that
MyAutoCompleteBox.SelectedItem
is an object that has my four values, if i hover this text in the debugger i can see the four values listed, however i don't know how to access these values in the code.
I tried
List<Codes> selected = MyAutoCompleteBox.SelectedItem as List<Codes>;
where Codes is my List. selected returns as null and empty every time. Is there a way to get to these values? Thanks!
If you want the listing of items used as the backing collection for the AutoCompleteBox try...AutoCompleteBox.ItemsSource.
Can you try:
Codes selected = MyAutoCompleteBox.SelectedItem as Codes;
or
Codes[] selected = MyAutoCompleteBox.SelectedItem as Codes[];
It means that you cannot convert whatever MyAutoCompleteBox.SelectedItem is to a List.