C# select item of combobox by ID - c#

I have this class
class ComboboxValue
{
public int Id { get; private set; }
public string Name { get; private set; }
public ComboboxValue(int id, string name)
{
Id = id;
Name = name;
}
public override string ToString()
{
return Name;
}
}
and I set my entries like this:
var list = Funktionen.LoadCustomers();
foreach (var item in list)
{
MyCombo.Properties.Items.Add(new ComboboxValue(item.ID, item.Name));
}
In another function, I will set a item in my combobox by customerID.
How can I do this?
Btw. I´m using Devexpress.
Thank you.

To programmatically select a value for the combo, set the ComboBoxEdit.EditValue property. Here is some sample code:
ComboBoxEdit.EditValue = 2; // select an item which ID = 2
Besides the Selected index you can use the SelectedItem property to select any item within the editor's item list. You need to assign an underlying data object to the SelectedItem property.
Alternatively, you can set its EditValue to '25' that is the ValueMember property value of the desirable item as shown in above example.
Reference these:
Select Item in ComboBoxEdit
how set combobox selected value

To select item in ComboboxValue class :
comboBox1.SelectedItem = comboBox1.Items.Cast<ComboboxValue>()
.Where(i => i.Name == dataGridView1.CurrentRow.Cells[5].Value.ToString()).Single();

var item = MyCombo.Properties.Items.FirstOrDefault(i => i.ID == yoursearchIDhere);
item will be combobox item which you want to get. If you look for it or not, let me know and explain clearly please
LoadCustomers() should return List also.

Try
MyCombo.SelectedItem = MyCombo.Items.SingleOrDefault(x => (x as ComboboxValue).Id == externalID)

Related

Is it possible to have a ComboBox DisplayMember set to a property of an object in a list?

I have a ComboBox being populated where each object in ComboBox.Items is a List of objects. Currently the ComboBox displays "(Collection)" for each Item.
Is it possible to have the ComboBox display a member of the first object in the List that comprises an Item of the ComboBox?
I am currently populating the ComboBox items by the following:
foreach(List<SorterIdentifier> sorterGroup in m_AvailableSorterGroups)
{
// There are conditions that may result in the sorterGroup not being added
comboBoxSorterSelect.Items.Add(sorterGroup);
}
//comboBoxSorterSelect.DataSource = m_AvailableSorterGroups; // Not desired due to the comment above.
//comboBoxSorterSelect.DisplayMember = "Count"; //Displays the Count of each list.
The value that I would like to have displayed in the ComboBox can be referenced with:
((List<SorterIdentifier>)comboBoxSorterSelect.Items[0])[0].ToString();
((List<SorterIdentifier>)comboBoxSorterSelect.Items[0])[0].DisplayName; // public member
You can create an object wrapper and override the ToString() method:
public class ComboBoxSorterIdentifierItem
{
public List<SorterIdentifier> Items { get; }
public override string ToString()
{
if ( Items == null || Items.Count == 0) return "";
return Items[0].ToString();
}
public BookItem(List<SorterIdentifier> items)
{
Items = items;
}
}
You should override the SorterIdentifier.ToString() too to return what you want like DisplayName.
Now you can add items in the combobox like this:
foreach(var sorterGroup in m_AvailableSorterGroups)
{
item = new ComboBoxSorterIdentifierItem(sorterGroup);
comboBoxSorterSelect.Items.Add(item);
}
And to use the selected item for example, you can write:
... ((ComboBoxSorterIdentifierItem)comboBoxSorterSelect.SelectedItem).Item ...
I could imagine a few ways to do this... you could create a class that extends List<T>, so you have an opportunity to define the value you'd like to display.
public class SortedIdentifier
{
public string Name { get; set; }
}
public class SortedIdentifiers : List<SortedIdentifier>
{
public string SortedIdentifierDisplayValue
{
get { return this.FirstOrDefault()?.Name ?? "No Items"; }
}
}
Then use the new class like this:
comboBox1.DisplayMember = "SortedIdentifierDisplayValue";
var list = new SortedIdentifiers { new SortedIdentifier { Name = "John" } };
comboBox1.Items.Add(list);

Can't iterate over ListView Items in Xamarin Forms

When I write this code:
ListView lv = new ListView();
foreach (ListViewDataItem item in lv.Items)
{
}
I get "the type or name ListViewDataItem could not be found"
Items is also not found under the lv object.
Basically I need to iterate through each row of the ListView and set a checkbox added using item template.
How can I accomplish that?
The correct way to loop through a listview is to access it's ItemsSource. Then you can cast the item into your view model and do stuff with it.
foreach (var item in lv.ItemsSource)
{
// cast the item
var dataItem = (ListViewDataItem) item;
// then do stuff with your casted item
...
}
I used a for loop to iterate over my listView ChildCount, assigned a var as the Tag of the GetChildAt as ImageAdapterViewHolder and then set my checkbox to false.
class ImageAdapterViewHolder : Java.Lang.Object
{
public ImageView SavedImage { get; set; }
public TextView Description { get; set; }
public CheckBox SelectImage { get; set; }
}
for (int i = 0; i < listView.ChildCount; i++)
{
var row = listView.GetChildAt(i).Tag as ImageAdapterViewHolder;
row.SelectImage.Checked = false;
}

How to bind additional data to an asp.net listbox control?

Using asp.net webforms I need to store additional data (other than the value field and the text field) in each list item.
Here's my datasource:
class Person
{
public string Name {get;set;}
public string PersonId {get;set;}
public string PersonType {get;set;}
}
List<Person> lista = GetPersons();
ListBox1.DataTextField = "Name";
ListBox1.DataValueField = "PersonId";
ListBox1.DataSource = lista;
ListBox1.DataBind();
The DataTextField is the Name. The DataValueField is the PersonId but I also want to bind the PersonType property so that when I retrieve the selected item from the user on the page postback:
ListItemCollection items = ListBox1.Items;
foreach (ListItem item in items)
{
if (item.Selected == true)
{
// Here I want to retrive also
// the PersonType attribute
string personType = item.????
}
}
How can I achieve that?
As mentioned in the comments, what you are suggesting is not readily done. ListItem only easily supports value and text.
As a work around, I'd suggest simply retrieving the person by their ID stored in the ListItem:
List<Person> lista = GetPersons();
ListItemCollection items = ListBox1.Items;
foreach (ListItem item in items)
{
if (item.Selected == true)
{
// Here I retrieve the PersonType by matching the ID
var person = lista.FirstOrDefault( person => person.PersonId == item.value);
// You may want to check for null...
string personType = person.PersonType;
}
}
Alternatively, you may want to make a function for obtaining a Person by PersonId directly, as that seems to be something you may need regularly.

I'm retrieving a value for a combobox with custom class items but I can't make it show the item

I wanted to have items and hidden values which I could call later so I used this Article to create my custom items.
But now that I'm calling one value I cannot make it show the proper item. The combobox stays null.
if (reader.HasRows)
{
reader.Read();
namebox.Text = reader["c_Name"].ToString();
lastbox.Text = reader["c_LastName"].ToString();
genderbox.SelectedItem = reader["c_gender"].ToString();
}
Here is what I add to my combobox and what I want to show accoring to what value I get from the reader
private void editcust_Load(object sender, EventArgs e)
{
genderbox.Items.Add(new ComboBoxItem("male", "1"));
genderbox.Items.Add(new ComboBoxItem("female", "0"));
}
Please let me know if I need to add more code or provide more information.
I'm a junior developer so please excuse my terrible mistakes and bad formulation.
First, override Equals and GetHashCode methods in your class:
public class ComboBoxItem()
{
string displayValue;
string hiddenValue;
//Constructor
public ComboBoxItem (string d, string h)
{
displayValue = d;
hiddenValue = h;
}
//Accessor
public string HiddenValue
{
get
{
return hiddenValue;
}
}
public override bool Equals(object obj)
{
ComboBoxItem item = obj as ComboBoxItem;
if (item == null)
{
return false;
}
return item.hiddenValue == this.hiddenValue;
}
public override int GetHashCode()
{
if (this.hiddenValue == null)
{
return 0;
}
return this.hiddenValue.GetHashCode();
}
//Override ToString method
public override string ToString()
{
return displayValue;
}
}
Then assign a new Item to the SelectedItem property:
genderbox.SelectedItem = new ComboBoxItem(string.Empty, reader["c_gender"].ToString());
When you assign a value to the SelectedItem property of ComboBox, it looks in it's items collection and tries to find an item that is equal to the assigned value. If it find an item equal to the value, that item gets selected. In the process, comparison is done by the Equals method of each item.
By overriding the method, you tell ComboBox to compare items using the "hiddenValue" field, so when you assign a new item with ite's hiddenValue set, combobox can find it in it's items collection. If you don't do that, equality comparison will be done using object references instead.
Use the DisplayMember & ValueMember properties of the ComboBox-Class and assign a DataSource.
ie. Your Data Class:
private class yourDataClass
{
public string DisplayMemberProperty { get; set; }
public int IDMember { get; set; }
}
Assign the datasource with values to the combobox
var dataSource = new ArrayList();
dataSource.Add(new yourDataClass() { DisplayMemberProperty = "Hello", IDMember = 1 });
dataSource.Add(new yourDataClass() { DisplayMemberProperty = "Hello2", IDMember = 2 });
dataSource.Add(new yourDataClass() { DisplayMemberProperty = "Hello3", IDMember = 2 });
this.comboBox1.DataSource = dataSource;
this.comboBox1.DisplayMember = "DisplayMemberProperty";
this.comboBox1.ValueMember = "IDMember";
Retreive the selected value...
var value = this.comboBox1.SelectedValue;
Agreed the question is unclear but if you mean that this call fails:
genderbox.SelectedItem = reader["c_gender"].ToString();
It's probably because that you need to use the same kind of value that you originally populated the list with.
i.e. if you populated it with instances of class x you need to set selectedItem to an instance of class x.

How to select items and move from one list box with datasource to another?

listBox2.DataSource = listBox1.SelectedItems;
listBox2.DisplayMember = "Descripcion";
listBox2.ValueMember = "Id";
After using the above code, I am not able to select one by one. Help!
some one please post codes to remove too
First, you have to define a model for your Listbox :
public class MyModel
{
public int Id { get; set; }
public string Description { get; set; }
}
After you can set items like this :
listBox2.Items.Add(new MyModel() { Id = 1, Description = "description" });
listBox2.DisplayMember = "Description";
listBox2.ValueMember = "Id";
And now, your listbox will show the description property. If you select an item, the SelectedValue in listbox2 will be the value of id property
if (listBox1.SelectedItem != null)
{
listBox2.Items.Add(listBox1.SelectedItem);
}
listBox2.DisplayMember = "Descripcion";
listBox2.ValueMember = "Id";
this is working fine....
Use SqlDataAdapter to dump data into the listbox from the data source and based on the data selected in one of the listbox use listbox.add() method, Use a "for" loop to dump the data from one listbox to another using an index.
for(int i=0; i<listBox1.SelectedItems.Count; i++)
{
listBox2.Items.Add(listbox1.SelectedItems[i].toString());
}

Categories

Resources