Set selected value in a SharePoint Dropdown list - c#

We are using SharePoint 2010 Foundation.
We have an item in a list that is a dropdown with values from another list.
When we access the list as a SharePoint list it works fine, we can select a value, save the list, the next time we access the list the correct value is selected.
We have programmed a form that will updated the list. When we pull up the form, select a value and save it, we can see by accessing the list directly that the value has been saved.
However, when we pull up the form again it is the first item in the list that is selected. Have tried storing the selected value is a temp variable before binding the list but have not been able to get it to work. Anyone know how to fix this?

We found a solution.
The trick was to get the number that is the first part of the ToString of the SPListItem, before binding the list.
Then use that number to set the selected value after the list is bound
Parameters:
SPListItem currentItem, string fieldName
Code:
string selectedValue = currentItem[fieldName].ToString().Substring(0,1);
//... Bind list
ddlLookup.SelectedValue = selectedValue;

Related

Can't manually add item to listbox that is populated iterating through a list

In a C# WinForms project I'm querying a database to get a list of values to populate a listbox with. The query populates a List and then I iterate through that to add the list items to the listbox.
lsNewValuesList = dbGetNewValueInfo.GetNewValuesDgvData(strNewValuesQuery);
foreach (string strItem in lsNewValuesList)
{
lsBxNewValues.Items.Add(strItem);
}
After that's done, I need to manually add an item to the top of the list, which I'm doing via, lsBoxNewValues.Items.Insert(0, "DELETE");. But when I run it I get the message,
Items collection cannot be modified when the DataSource property is set.
Here is a screenshot of the error (to clarify some questions):
Looking into that I'm understanding that error arises when the listbox is populated with a datasource, but I'm just populating it from a string list. Is that, technically, a datasource then?
How do I accomplish what I'm trying to do?
[UPDATE]
Okay, I've done some fiddling around, albeit without resolving my issue, and I've tried the following:
lsNewValuesList = dbGetNewValueInfo.GetNewValuesDgvData(strNewValuesQuery);
lsNewValuesList.Insert(0, "DELETE");
lsBxNewValues.DataSource = lsNewValuesList;
//foreach (string strItem in lsNewValuesList)
//{
// lsBxNewValues.Items.Add(strItem);
//}
Instead of inserting "DELETE" in the ListBox (which is what I was originally trying and was causing the error), I inserted it at index 0 of the List, the datasource of the ListBox, and then set the ListBox's datasource. "DELETE" is showing up in the ListBox, but it's getting alphabetized along with the rest of the items. I'm not doing any sorting of the list or the ListBox - I'm using ORDER BY in the database query, however. CyberZeus suggested I use Refresh() on the ListBox, but that didn't have any effect.
Yes, a list of strings is a datasource.
You have to add the data retrieved from the database to the datasource of the ListBox. You may need to refresh the ListBox.

Select only part of combobox string or store two values per index possible?

Here's what I have so far:
I have a ComboBox that I'm filling via pulling some playlists from a C# Spotify API.
I get the values - playlist.Id and playlist.Name from a playlist object.
Each playlist populates its Id and Name into a row in my ComboBox dropdown so the user can see a list of their personal playlists.
Next, the user chooses their selected playlist in the ComboBox and can pull a ListView of all the tracks (artists etc).
This step requires selecting the user's ComboBox selection but I can only use the playlist.Id value. However, I need to include the playlist.Name in the box so the user knows what playlist to select.
Obviously, this is pulling the name AND Id from the selected ComboBox option and I can't use these two values for the tracklist pull. I only want the Id string.
Is there any way to select only part of the ComboBox string (just the ID) or assign a value and a text separately to one row in my ComboBox?
Appreciate I'm probably doing this a silly way.
I've also tried:
Creating a separate array with just the playlist Id's and trying to use the selected index of ComboBox to match with the array index (only containing the id and not the name).
userPlaylists.Items.ForEach(playlist =>
{
playlistArray[i] = playlist.Id;
comboPlaylists.Items.Add(playlist.Name + playlist.Id);
i++;
});
There are ways to extract part of the ComboBox text but this isn't the right way. Instead, you should use Data Binding. Set the DataSource, DisplayMember, and ValueMember properties of the ComboBox so that you can later access the SelectedValue which would be the ID.
Here's an example:
comboPlaylists.DisplayMember = "Name";
comboPlaylists.ValueMember = "Id";
// I'm assuming that `userPlaylists.Items` is a generic list.
// Otherwise, you might want to use `userPlaylists.Items.ToList()` instead.
comboPlaylists.DataSource = userPlaylists.Items;
Now, when you want to get the Id of the selected PlayList, you can use something like this:
var id = (int)comboPlaylists.SelectedValue; // Assuming the ID is an integer.

Set value of Asp DropDown List is not Working

I have an asp.net DropDownList that I use to filter a RadGrid. After the filter has been applied I can click on a row to edit the record on a separate page. I have a requirement to provide the ability, if the wrong row was selected, to return to the previous search page and display the same records with the same filter. I have taken care of the return and showing the same filtered records.
I also need to show the same value in the dropdown list that was chosen to create the filter. I am trying to do this using a session variable. The session variable gets created on the search click and I am trying to select the same item from the drop down using this code when the user returns to the search page.
string value = (Session["ComplaintType"] != null) ? Session["ComplaintType"].ToString() : String.Empty;
ddlComplaint.Items.FindByValue(value).Selected = true;
It is not working ant I get this error message: Object reference not set to an instance of an object.
Not sure why I am getting that error the string value is equal to the text value of the item selected from the Drop down??
I was able to solve this problem by setting the dropdownlist selected value in the BindDropDown method where I bind the database to the database. Once it was bound I could select a value from the database as the default value.

Dynamically populate an InfoPath DropownList with managed code

I have an InfoPath form with custom C# code, and a Sharepoint list. I have a dropdownlist in the InfoPath form that I want to populate with a certain field from the Sharepoint list (I want the InfoPath dropdownlist to contain this field's value from every item in the Sharepoint list. I can successfully get the list of values I need from Sharepoint in my managed code, but I do not see how I can get these values into the dropdownlist (either bind to the list, or add each item in the list one by one). I thought I could modify the XML of the dropdownlist to insert my items, but the XML only contains the first item in the dropdownlist:
<my:RelatedRiskID xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-07-20T18:12:59">Option 1</my:RelatedRiskID>
I feel like this should be possible, but I can't find any resources on how to do it. Thanks in advance for the help.
If you plan to populate your dropdown list with a SharePoint list then you need to
create a data connection to the said SharePoint list
in the dropdown list Data tab, get the data externally and select the said data connection

Different Value Member, same control

Edit 1
I believe my problem stems from the following. The function that fills the dropdown portion sets the Display Member to the CountyName. Then when I try and set the SelectedText or EditValue, as has been suggested, that function only returns the CountyID which it try's to match to something in the DropDown list DisplayMember. I need it to match it to something in the ValueMember list.
Using the following I got it to work but it is a HACK and I'd greatly appreciate finding a real solution.
lkuResidenceCounty.ItemIndex = Convert.ToInt32(row["ResidencyCountyID"].ToString());
Original Post
I have a lookup box(DevExpress) on a member form that I fill the possible values in from the DB with this code -->
lkuResidenceCounty.Properties.DataSource = ConnectBLL.BLL.Person.CountyList();
lkuResidenceCounty.Properties.PopulateColumns();
lkuResidenceCounty.Properties.DisplayMember = "CountyName";
lkuResidenceCounty.Properties.ValueMember = "CountyID";
lkuResidenceCounty.Properties.Columns[0].Visible = false;
lkuResidenceCounty.Properties.Columns[2].Visible = false;
lkuResidenceCounty.Properties.Columns[3].Visible = false;
This works just fine as the CountyName is displayed as expected.
However, When I try and load an existing member's value for this field using the below, which is part of a function that takes a row from the DataSet -->
lkuResidenceCounty.Properties.ValueMember = row["ResidencyCountyID"].ToString();
I get a blank box. I have stepped through the code and the correct ID is being returned for the member.
Unfortunately the stored procedure to fill the dropdown options pulls from a Maintenance Table with the columns "CountyName" & "CountyID". So that is correct. Unfortunately, the stored procedure to load a specific person's current county pulls from the Person Table where there is a column called "ResidencyCountyID". It is so named because there is also a "ResponsibilityCountyID" column.
I need a way for them both to coexist, any solutions?
Thanks!
DisplayMember and ValueMember are used to populate the control with the list of selectable values. To set the selected value of a populated LookUpEdit control, set its EditValue property:
lkuResidenceCounty.EditValue = row["ResidencyCountyID"].ToString();
In response to your edit: According to the documentation:
The currently selected row determines values for the editor's edit value and display text. The value for BaseEdit.EditValue is obtained from the RepositoryItemLookUpEditBase.ValueMember field, while the text to display in the edit box is obtained from the RepositoryItemLookUpEditBase.DisplayMember field of the selected row.
When you change BaseEdit.EditValue,
the editor locates and selects the row
whose
RepositoryItemLookUpEditBase.ValueMember
field contains the new value. The text
in the edit box is changed to reflect
the newly selected row.
I don't use these controls but it sounds to me that it shouldn't be working as you described. I think the ToString() is the problem because EditValue accepts an object so it's probably expecting an int for the value. Try:
lkuResidenceCounty.EditValue = (int)row["ResidencyCountyID"];
The ValueMember property tells the list what field to pull from when setting the Value property. Once you've set the ValueMember to "CountyID", then when the list is Databound, it will set all the list items' value properties to their respect objects' CountyID fields.
Hence, you should not do:
lkuResidenceCounty.Properties.ValueMember = row["ResidencyCountyID"].ToString();
but rather
lkuResidenceCounty.Properties.ValueMember = "CountyID";
was perfectly fine, as long as you've correctly identified the field you're trying to databind. Once the list get's databound you should see the results you're expecting.
However, after looking at your code, it seems that you're mismatching your field. In one place you're using ResidencyCountyID and in another you're using CountyID. That is most likely your source of confusion. Figure out what the actual field name is and make sure you set the ValueMember to that.
UPDATE
After reading your comment, what your looking for is the SelectedValue property. The SelectedValue property tells the list to force the selected value to whatever input you give it.
Essentially you have two things going on here. ValueMember tells the list what to use as the value from your data source, and SelectedValue which tells the list what the currently selected value should be.
Why is it that you need the same LookUpEdit to have two value members? Is it being used standalone or in a grid? If standalone, you could swap out the two repository editors depending on the current row. But, are there more than 2 possible values for the ValueMember? That would also complicate things.
UPDATE
Looking at your edit, I think I understand what's going on a little more. So, you don't wish to change your ValueMember (which refers to a data column), but rather to change the value of the editor? If so, then you should definitely use EditValue (not SelectedText, which I don't believe is meant to be set), and assign it to row["value_field"] like so:
lkuResidenceCounty.EditValue = row["ResidencyCountyID"];
What happens when you do that?

Categories

Resources