Prevent ComboBox from updating displayed text when datasource changes - c#

I have a ComboBox which is bound to a BindingList.
The background is that I want to have an option to choose from the dropdown-list (as a suggestion), but I don't want to change the displayed text if the ItemsSource changes (for example, when I'm adding an item to the BindingList).
Is there a way to prevent the ComboBox to update the displayed text when the items source is changed?
Some code:
this.comboBox1.DataSource = database.ListItems; // database.ListItems is of type BindingList<string>
public void update_ListItems(BindingList<string> ListItems)
{
ListItems.Add("Item"); // Causes an update of the displayed text in the ComboBox
}

If you need to break the binding between ComboBox and data source you can convert the BindingList to List like this:
this.comboBox1.DataSource = database.ListItems.ToList();
This will prevent updating of ComboBox items when you change the BindingList

Related

keep combobox text synchronized with changed item text

I have a Combobox with several items (C# Labels because I want to change individual text colors). Within callbacks, when editing some textboxes, I change the color and/or text of the items. When I click on the combobox I see that the items in the list have the correct color/text. However, the color/text changes of the selected item are not directly reflected in the shown combobox text. How can I achieve that?
I tried to set the Text property of the combobox itself: no effect. Also setting the selected item to an empty Label and then set it back to the correct Label has no effect. If I set SelectedIndex to -1 and then back to the correct selected index it works and the shown text is updated, but this triggers the SelectionChanged callback which I do not want. I could first detach the SelectionChanged callback from the combobox and then attach it again, but this is in my opinion very ugly programming.
Maybe I am missing something simple...
Edit
I tried binding, following the suggestion of SLak:
List<Label> labels;
MyComboBox.ItemsSource = labels;
The result is still the same. Suppose index 0 is selected. When I change the corresponding Label:
labels[0].Contents = "new content";
then it is not reflected in the selected text of the combobox. When I click the combobox I can see the new text in the unfolded list, but only when I change the selection and then go back to index 0 the new text is shown by the combobox as the selected item. That synchronization should be autmatically.

Set datasource of combobox in devexpress in gridview c#

I am trying to create a gridview with list as you can see
I add the item of the list using this code :
private void frmDocument_Load(object sender, EventArgs e)
{
gridControlDocument.DataSource = new BindingList<Document>(_documentRepository.Get().ToList()) { AllowNew = true };
DisciplineList.Items.Add("ali");
}
but i need to get data from the database ,but the DisciplineList doesn't have the datasource property .
The ComboBoxEdit control is not meant to be bound to a data source. You would need to either loop through your DisciplineList collection and add each item manually, or use the LookUpEdit control, which does offer a data source property.
In your case, you can add a RepsositoryItemLookUpEdit to the GridControl (See: Assigning Editors for In-Place Editing) and set its DataSource property to your collection. Additionally, set the ValueMember and DisplayMember properties to a property within the Discipline class.

.NET C# ComboBox default text

I have a combobox populated via Datasource and when the application runs it shows the first item instead of the default text setup in the properties.
How can I accomplish this?
EDIT:
The data comes from an API:
loteamentos = JsonConvert.DeserializeObject<List<Loteamento>>(dataObj.Result);
and once I have the data, I populate de ComboBox:
cb_loteamentos.DataSource = loteamentos;
cb_loteamentos.ValueMember = "id";
cb_loteamentos.DisplayMember = "nome";
Because you are binding your combobox from a data source, it is going to populate the items from the data source and anything set via property pane will be overridden.
You need to add that item as first item in your data source also, to make it appear in combo.
While assigning the DataSource to the comboBox the current associated collection and hence the Default text will be changed. So Assigning default text after assignment of the DataSource will solve your issue:
// Bind the combobox
comboBox1.SelectedIndex = -1;
comboBox1.Text = "Please select any value";
On the form load:
combobox1.SelectedIndex = 0

Items in WinForms ComboBox not updating when DataSource values change

I have a ComboBox bound to a List via a DataSource. For some reason, when the datasource items change, the items in the combo box don't seem to automatically update. I can see in the debugger the datasource contains the correct items.
There are lots of answers on StackOverflow about this, but most are either unanswered, don't work for me, or require changing from using Lists to BindingLists which I cannot do this instance due to the volume of code which uses methods BindingLists don't have.
Surely there must be a simple way of just telling the ComboBox to refresh it's items? I can't believe this doesn't exist. I already have an event which fires when the Combo needs to be updated, but my code to update the values has no effect.
Combo declaration:
this.devicePortCombo.DataBindings.Add(
new System.Windows.Forms.Binding("SelectedValue",
this.deviceManagementModelBindingSource, "SelectedDevice", true,
DataSourceUpdateMode.OnPropertyChanged));
this.devicePortCombo.DataSource = this.availableDevicesBindingSource;
Code to update the combobox:
private void Instance_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "AvailableDevices")
{
// Rebind dropdown when available device list changes.
this.Invoke((MethodInvoker)delegate
{
devicePortCombo.DataSource = AvailableDevicesList;
devicePortCombo.DataBindings[0].ReadValue();
devicePortCombo.Refresh();
});
}
}
You are not binding the DataGridview's DataSource to same BindingSource object in your case this.availableDevicesBindingSource which bound first time. but later you are binding to different object AvailableDevicesList. again you are using another binding source for SelectedValue i.e this.deviceManagementModelBindingSource.
use one BindingSource only, may solve your issue

ListBox CollectiovViewSource.Filter changes selected item?

I have a ListBox that has items defined by a data template.
I also have a textbox where the user can type and I take the text and filter items in the listbox using the Filter property on the CollectionviewSource...as:
ICollectionView listBoxView = CollectionViewSource.GetDefaultView(myListBox.ItemsSource);
listBoxView.Filter = ((item) => { // Filtering logic };
This works great. However, this also clears out the SelectedItem of the ListBox, which I dont neccessarily want to. So before the user starts typing in the listbox, lets say that an item is already selected, the filtering logic seems to clear out the selection.
Is there a way to avoid that...?
Thanks!

Categories