.NET C# ComboBox default text - c#

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

Related

C# how to set a combo box value based on saved property settings using string

I am new on C# programming and wants to save the last selected item from a combo box using save property settings method.
I am able to save the last selected item using below code.
Properties.Settings.Default.model = modelBox.SelectedItem.ToString();
Properties.Settings.Default.Save();
But cannot select it using below code (This is on initialize winform).
this.modelBox.SelectedText = Properties.Settings.Default.model;
Any suggestion would be ok, thanks in advance!
Use the selected Index instead of the actual text:
//Save
Properties.Settings.Default.model = modelBox.SelectedIndex;
Properties.Settings.Default.Save();
//Restore
this.modelBox.SelectedIndex = Properties.Settings.Default.model;
The problem with your code is that SelectedText is not really ment to be used to set the Selected Item. Its more like a readonly property in your situation. While setting the Selected index actually changes the selection of the combobox

Populate drop down list with current user value as the selected drop down list option asp

I would like to know the best approach on populating a drop down list with all database values, AND have the current value for a user be the selected index. I have the drop down list being populated with the database values, but I'm not sure on how you select. Thank you
Set the DataSource Property of the Dropdown control to the dataSource. The DataSource can be a List or any other kind of DataSource.
Set the DataTextField property, which represent the text, which is displayed for each item in the dropdown list.
Set the DataValueField property, which represent the Unique Value for each item in the dropdown list.
Call the DataBind method of the DropdownList.
To set the selected item, set the SelectedIndex property of the dropdown to the index which needs to be selected.
Following code snippet shows how it can be done..
private void LoadDropdown()
{
dropdownList.DataSource = YourDataSource;
dropdownList.DataTextField = "DisplayPropertyName";
dropdownList.DataValueField = "ValuePropertyName";
// Bind the data to the control.
dropdownList.DataBind();
// Set the default selected item, if desired.
dropdownList.SelectedIndex = indexOfItemWhichShouldBeSelected;
}

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.

Prevent ComboBox from updating displayed text when datasource changes

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

How to display blank item in the combobox?

For a Combobox, I am getting a list of values from the System.Collections.ObjectModel.Collection.
I am assigning the values like this:
this.cmbSqlServer.DataSource = this.SqlInstancesCollection;
I don't want to see the first item in the list in the Combobox, unless I selected it.
How do I display a blank field in the Combobox when nothing is selected?
If I understand correctly you just need to reset the SelectedItem. Just set SelectedIndex to -1.
this.cmbSqlServer.DataSource = this.SqlInstancesCollection;
this.cmbSqlServer.SelectedIndex = -1;

Categories