How to display blank item in the combobox? - c#

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;

Related

ComboBox doesn't load blank value when using ComboBox.SelectedItem = -1

I have a comboBox which correctly loads the List of data that I've queried from the DB, my only issue is that I can't seem to make it load blank and force the user to select from the list (which then pushes the actions under the SelectedIndexChanged() method). Through my searches, I've seen where I can simply change the SelectedItem value to -1, and that should load the comboBox on a null choice, but when I use this code I see no difference. The comboBox still loads the list correctly, but the first entry in the list is still displayed.
private void loadPatientList()
{
comboBox_PatientSelect.DataSource = patientList.Distinct().ToList();
comboBox_PatientSelect.DisplayMember = "displayFullName";
comboBox_PatientSelect.ValueMember = "patientID";
comboBox_PatientSelect.SelectedItem = -1;
}
I appreciate any help - and will be happy to provide more information if needed. Thank you in advance!
The property you're looking for is SelectedIndex. So make it comboBox_PatientSelect.SelectedIndex = -1;
From MSDN:
This property indicates the zero-based index of the currently selected item in the combo box list. Setting new index raises the SelectedIndexChanged event.
SelectedIndex, SelectedValue, and FormattingEnabled are related as follows:
If FormattingEnabled is false, SelectedIndex will not be set to -1 when SelectedValue is blank.
If FormattingEnabled is true, SelectedIndex will be set to -1 when SelectedValue is blank.
NOTE:
To deselect the currently selected item, set the SelectedIndex to -1. You cannot set the SelectedIndex of a ComboBox item to -1 if the item is a data-bound item.

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;
}

SelectedItem of a variable combobox

There is a combobox which i fill with this loop
foreach (Machine.Types machine in machineList)
{
cbMachineGUI.Items.Add(machine);
}
after that i want the selected index to be one specific machine.
string machineComboBox = SettingsManager.getParameter("MachineType");
cbMachineGUI.SelectedItem = machineComboBox;
The parameter is correct and set, but the selecteditem of the combobox is always nothing.
if i set the machines in the properties of the combobox (not via the loop) it works. but i need the combobox to be variable.
any suggestions?
The problem is that what you put in Items and what you set SelectedItem to are different types.
You are filling the Items collection with Machine.Types instances, and setting SelectedItem to a string instance.
Using IndexOf like other answers suggest will not help, as this will not do anything that setting SelectedItem does not already do. It still won't find machineComboBox in the Items collection, just like it can't find it now.
You need to use matching types, so do one of these things (depending on how else you use the values in the combobox):
Convert Machine.Types to a string when filling the collection:
cbMachineGUI.Items.Add(machine.ToString());
Convert machineComboBox into an instance of Machine.Types that will match the one in Items when setting SelectedItem - how to do it depends on what Machine.Types is
Find the correct item yourself when setting SelectedItem:
cbMachineGUI.SelectedItem = cbMachineGUI.Items
.OfType<Machine.Types>()
.FirstOrDefault(item => item.ToString() == machineComboBox);
Either way, you must make a conversion between these two types somewhere.
Instead of setting SelectedItem, I suggest you find the item's index and set the selected index.
Something like this:
string machineComboBox = SettingsManager.getParameter("MachineType");
int itemIndex = cbMachineGUI.Items.IndexOf(machineComboBox);
cbMachineGUI.SelectedIndex = itemIndex;
You could try the following:
cbMachineGUI.SelectedIndex = cbMachineGUI.Items.IndexOf("MachineType"); // or whatever you want to select
It could be possible that the item you are trying to set doesn't present in combobox item list and since you haven't actually selected anything it sets to nothing. To check if the item does exist do below
string machineComboBox = SettingsManager.getParameter("MachineType");
if(cbMachineGUI.Items.IndexOf(machineComboBox) >= 0)
cbMachineGUI.SelectedItem = machineComboBox;
Quoting from MSDN documentation:
When you try to set the SelectedItem property to an object, the
ComboBox attempts to make that object the currently selected one in
the list. If the object is found in the list, it is displayed in the
edit portion of the ComboBox and the SelectedIndex property is set to
the corresponding index. If the object does not exist in the list, the
SelectedIndex property is left at its current value.The ComboBox class
searches for the specified object by using the IndexOf method.
Check ComboBox.SelectedItem for more information.

C# - Change index of ComboBox item?

I have a combobox that I want to be able to add items to the beginning of. For example, when you click it, you get 1,2,3 , but I want to be able to add an option, 0, so that when you click it you get 0,1,2,3.
Is this possible without rebuilding the combobox?
Just use the Insert method of the Items property of the combo box:
myComboBox.Items.Insert(0, "New item at top");
Yes, you can use the Insert() method on the Items property of your ComboBox object.
var comboBox = new ComboBox();
comboBox.Items.Add("1");
comboBox.Items.Add("2");
comboBox.Items.Add("3");
comboBox.Items.Insert(0, "0");

C# Winforms - How can I dynamically set the selectedItem of combo box?

I cannot seem to figure out how to dynamically change the selected item in a combo box. I am trying this:
myComboBox.SelectedItem = item.Id;
Here item.Id is a int that corresponds to valid ValueMember that is bound to the combobox. However the combobox remains unchanged. I have trying invalidating the control after changing the selected item. What's the trick?
Thanks
try SelectedValue instead..
myComboBox.SelectedValue = item.Id;
You can use either of these:
SelectedIndex Property
ComboBox attempts to make that object the currently selected one in the list. If the object is found in the list, it is displayed in the edit portion of the ComboBox and the SelectedIndex property is set to the corresponding index. If the object does not exist in the list, the SelectedIndex property is left at its current value.
SelectedItem Property
Get or Set A zero-based index of the for selected item.

Categories