I am trying to set the value or text of a drop down to a value being pulled by the database, this is being passed through an Entity and when I debug i see the right hand side part of the code has the string value i want to match on the drop down but still when I do a step in and goes to the next line the left hand side still selects the -- Select -- item
I am trying to assign it as follows
ddlMyDropDown.SelectedItem.Text = Clients.MerchandiseType.ToString();
WHEN STEPPING THROUGH IT
ddlMyDropDown.SelectedItem.Text contains "--Select --"
Clients.MerchandiseType.ToString() contains "Sporting Goods"
ddlMyDropDown has an Item named "Sporting Goods" but it never selects it when I use the code above , I am not sure why. Is there anyway to achieve this?
I am not trying to change change any values , but to set the ddlMyDropDown to the Text in Clients.MerchandiseType.ToString(). The drop down will always have the text being passed on Clients.Merchandise.ToString();
Thank you
Not sure of the method names at the moment as i'm not at a pc with visual studios but here's some hints.
var text = Clients.MerchandiseType.ToString();
var item = ddlMyDropDown.Find(text); //Some method that returns a list item
ddlMyDropDown.SelectedItem = item;
Hope this helps.
If theres not a better answer tomorrow I will post a full solution.
Try
ddlMyDropDown.SelectedIndex = Combox1.FindStringExact(Clients.MerchandiseType.ToString());
Not sure but I think you can't change the Selected Item by changing SelectedText.
You have to set
ddlMyDropDown.SelectedIndex = i
or
ddlMyDropDown.SelectedItem = item
Use one of the following:
ddlMyDropDown.SelectedValue = Clients.MerchandiseType.ToString();
OR
ListItem item = ddlMyDropDown.Items.FindItemByValue(Clients.MerchandiseType.ToString());
if (item != null)
item.Selected = true;
Related
In a dropdown I've got a list of country of this type:
Text: "Italy" Value :"IT"
I need a copy of one of the country in the top of the list referred to the current language of the portal I'm working in, so the user can both select the first or the one into the list. Is just a hint we can say.
So I've just added a copy in this way:
cmbNazione.Items.Insert(0, cmbNazione.Items.FindByText(valori.Rows[0]["M_SOAAuthorityCountry"].ToString().ToUpper()));}
This code works fine. I got my duplicate value and I can select it without problem.
I am stuck when I already have a country that I have to set into the dropDown.
I just wrote that:
cmbNazione.Items.Insert(0, cmbNazione.Items.FindByText(valori.Rows[0]["M_SOAAuthorityCountry"].ToString().ToUpper()));
cmbNazione.ClearSelection();
cmbNazione.Items.FindByText(valori.Rows[0]["M_SOAAuthorityCountry"].ToString().ToUpper()).Selected = true;
The problem is that I receive the error: Cannot have multiple items selected in a DropDownList.
In fact if I check I got both the list Items (first, and the identical in the list) with the prop : selected = true. If I try to make one false both change to false. I cannot use them separately.
I can't understand why I can use them correctly when I select manually, as a user, but not when I try to select them through code.
I also tried something like :
cmbNazione.SelectedIndex = cmbNazione.Items.IndexOf(cmbNazione.Items.FindByText(valori.Rows[0]["M_SOAAuthorityCountry"].ToString().ToUpper()));
But nothing. I'll every time have multiple items in the list of the selected
You should not have a duplicate value as it will confuse the user (if the value is on 2-3 position already) and architecture as retrieval may lead to 2 values being selected.
Your use case seem like you are offering the most commonly used value as the first one and then the remaining. If this is the case, follow the approach outlined below.
Find the ListItem required, then remove it from the list and add on the 1st position. Then mark it as selected.
var preferredItem = cmbNazione.Items.FindByText(...);
if (preferredItem != null) {
cmbNazione.Items.Remove(preferredItem);
cmbNazione.Items.Insert(0, preferredItem);
cmbNazione.SelectedItemIndex = 0;
}
This way, there will be single item being selected and preserve the sanctity of the item (underlying value etc) while still allowing user to have the preferred one on top of list. You can chose to have this as 'pre-selected' or let user select it explictly.
To be more specific...
I use this to select a value from a dropdown list during a coded ui test... this extracts value from a specific row in a csv and selects it in the DDL.
this.UIMap.MarketCodeType2Params.UIMarketsComboBoxSelectedItem = TestContext.DataRow["IFAGeneralMarketCodeType"].ToString();
The value is
ABC123
When the test runs... the first value it finds in the Application DDL which is the default value is
ABC123 222
And it selects it.. in VS2010, it looked for the exact value it is being compared with. Now in 2012, it's trying to be smart and is selecting the first one that contains the value is has...
Any help would be appreciated..
I tried adding if's and do while's, but they all assert on the value coming from CSV and not value selected. When i managed to assert on the value selected, the test run kept looping, selecting the wrong one then opening DDL again(when it realizes its supposed not to) and selects it again..
The selection function in VS2012 is messed...
Here is how it was solved, a very terrible way, but i can't do any better being on limited time.
uICcmbMarketsComboBox.SelectedItem = this.ClientCreationRecParams.UICcmbMarketsComboBoxSelectedItem;
int x = 0;
for (int i = 0; i < uIMarketsComboBox.Items.Count; i++)
{
if (uIMarketsComboBox.Items[i].Name.ToString() != "ABC123")
{
continue;
}
else x = i;
}
uIMarketsComboBox.SelectedIndex = x
Why'd they have to break ComboBox Selection in VS2012?!.... :(
During the construction of user control I populate the combobox with some data. Works fine. I have Status.ID as valuePath and Status.Name as displayPath.
cmb.ItemsSource = dbEntities.Status
The comobox will be used as a filter control and I need to insert some value for "All", which will be used as the empty filter.
First I tried a funny solution:
ObjectSet objectSet= dbEntities.Status;
Status stAll = new Status();
stAll.ID = -1;
stAll.Name = "All";
objectSet.AddObject(stAll);
cmb.ItemsSource = objectSet;
For some reason the object is not added to the objectSet. It didnt throw any exception either.
Then I tried to insert it manually to the first index but I got the error:
"Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead."
My code looked like:
cmb.ItemsSource = entities.Status;
cmb.Items.Insert(0,"All");
Both didnt work. What would be the easiest way to add that line to the combobox? The error message got me confused. I am not sure how to use the ItemsSource for such a purpose.
edit: I did not have enough rep to answer my own question so here is the working code. Thanks Craig again.
CompositeCollection comp = new CompositeCollection();
comp.Add(new CollectionContainer {Collection = dbEntities.Status});
Status stAll = new Status();
stAll.ID = -1;
stAll.Name = "All";
comp.add(stAll);
cmb.ItemsSource = comp;
//do whatever filter you want when the selected value is -1
There are a couple different problems with what you are trying to do. You can't manipulate the Items when you are using the ItemsSource, instead you have to go through the object that is set to the ItemsSource. That is what the error message is about in the second part. Its because when you set the ItemsSource the Items value is unused it is not populated with the values of the ItemsSource.
I'm not familiar enough with the ObjectSet class to know why the first case is not working. However, it seems awkward to add an item to your values you are pulling from somewhere else, just to have the all case. The better solution is to use a null value to represent nothing. Unfortunately, there is no built in way to do this in WPF. However, there is a fairly easy solution of using an Adaptor do do this. I have used this solution a NullItemSelectorAdaptor, that enables null as a selection even if null is not in the list. All you have to do is wrap your combobox in the NullItemSelectorAdapter and null will be added as a value. The blog post explains everything pretty clearly, so I won't repeat it. You than can setup your filter so that null equates to no filtering.
I'm trying to simply add a simple text or hyperlink field to a list item in sharepoint 2007.
I can add the field no problem:
list.Fields.Add("MyField",SPFieldType.Text, false);
And it shows up fine on my list items.
However no matter which way I try, I can't programmatically set a value for the field. I tried:
list.items[0]["MyField"] = "text";
and I tried loading into a field:
SPField field = list.items[0].Fields["MyField"];
and setting it there, and setting the default value and updating, but nothing what so ever happens.
I always finish my code blocks with list.update(); or if I'm operating on the item itself item.update(); so I'm not at least missing that. Can anyone tell me what I'm doing wrong?
Thanks
Try:
SPListItem item = list.items[0];
item["MyField"] = "text";
item.Update();
Although it seems equivalent, the above code is not the same as:
list.items[0]["MyField"] = "text";
list.items[0].Update();
For more information, see here and here for people who have documented the same behavior.
Could you try this for adding a new field and setting a default value? Untested code. let me know how it goes.
SPFieldText fldName = (SPFieldText)list.Fields.CreateNewField(SPFieldType.Text.ToString(), "mycolumn");
fldName.DefaultValue = "default";
list.Fields.Add(fldName);
list.Update();
I've always found the best route is to get a reference to the list item directly and update specifically, as opposed to using the indexer route. Just like rich's first example mentions.
http://www.sharepointdevwiki.com/display/public/Updating+a+List+Item+programmatically+using+the+object+model
From all the discussion above it appears that you are trying to set the field value in a list event handler and you are setting the value in item adding or item updating event. If this is the case then you need to consider AfterProperties. Remember we have *ing and *ed events and in case of *ing events we need to work with BeforeProperties and AfterProperties.
I hope this helps!
I'm new to C# and I'm starting to learn how to program
I'm learning to program into the Visual Studio Microsoft Edition where I use the WindowsApplication instead of the Console.
While trying to make this code, I encountered this command:
Selected Index and Selected Item
and I would like to know the difference between the two.
I'm quite confused now with my code. The code I'm trying to do is adding and deleting text in the listbox.
Thanks for your help.
Additional question: in my code I have this line:
int listBoxSelectedItem = listBox1.SelectedIndex;
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
I would like to understand this part:
The first line, has a variable called " listBoxSelectedItem" with a type "int". The position of the item you selected will be store to the variable called "listBoxSelectedItem". Is that correct?
The second line is, the "listBox1.SelectedIndex" is the information that is being pass through to the method, "RemoveAt" Is my understanding here correct?
Thanks
Selected item will return the object that is selected. Selected index returns the location in the list as an int.
For example you may have a list of strings:
Cat
Dog
Hamster
Horse
If you select "Dog" from this list them the SelectedItem property is the string "Dog" while the SelectedIndex is 1 (indexes are zero based, so the first item is 0, second 1 etc.)