Combobox: Adding Text and Value to an Item (No Binding Source)

ComboBox: Adding Text and Value to an Item (no Binding Source)

You must create your own class type and override the ToString() method to return the text you want. Here is a simple example of a class you can use:

public class ComboboxItem
{
public string Text { get; set; }
public object Value { get; set; }

public override string ToString()
{
return Text;
}
}

The following is a simple example of its usage:

private void Test()
{
ComboboxItem item = new ComboboxItem();
item.Text = "Item text1";
item.Value = 12;

comboBox1.Items.Add(item);

comboBox1.SelectedIndex = 0;

MessageBox.Show((comboBox1.SelectedItem as ComboboxItem).Value.ToString());
}

How add items(Text & Value) to ComboBox & read them in SelectedIndexChanged (SelectedValue = null)

You can take the SelectedItem and cast it back to your class and access its properties.

 MessageBox.Show(((ComboboxItem)ComboBox_Countries_In_Silvers.SelectedItem).Value);

Edit You can try using DataTextField and DataValueField, I used it with DataSource.

ComboBox_Servers.DataTextField = "Text";
ComboBox_Servers.DataValueField = "Value";

C# combobox adding multiple items text and values from database dynamically

Your error is in the Item.Insert method where the first parameter should be the position in the already existing items collection where you want to insert the new value and not the Item's value.

Instead of using this manual loop you could simply set the combo datasource to a DataTable and set the DisplayMember and ValueMember properties

string connStr = "My Connection string";
using(SqlConnection conn = new SqlConnection(connStr))
{
string strQuery = "SELECT * FROM tbl_Companies ORDER BY id ASC";
SqlCommand cmd = new SqlCommand(strQuery, conn);
conn.Open();
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader())
cmbCompany.DisplayMember = "name";
cmbCompany.ValueMember = "id";
cmbCompany.DataSource = dt;
}

Consider also that once you set the Datasource property in this way you shouldn't manually insert new items, instead you add elements to the underlying DataTable and refresh the combo datasource binding

c# ComboBox, save item with value and text

You could (should) set the displaymember and valuemember in another place, but...

public Form1()
{
InitializeComponent();
comboBox1.DisplayMember="Text";
comboBox1.ValueMember ="Value";
comboBox1.Items.Add(new ComboboxItem("Dormir", 12));
}

binding combobox with text and value

To use the DisplayMember and ValueMember properties you need to set the DataSource property of the combo. The easiest way is to load a DataTable and use that instance as DataSource

using (OracleConnection conn = new OracleConnection(connstr))
using (OracleCommand cmd = new OracleCommand(cmdtxt, conn))
{
conn.Open();
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
CBOfficeCode.DataSource = dt;
CBOfficeCode.DisplayMember = "Column1";
CBOfficeCode.ValueMember = "Column2";
}

Set combo box visible text to nothing after populating it via a binding source

I can see two different approaches.

First

Change this method to add a new item to your comboBox:

public void Populate_SchemesID_Combolists(Dictionary<int, string> comboSource)
{
comboSource.Add(0, ""); //To be the first item displayed
// or
comboSource.Add(0, "Select the Scheme"); // To be default
// maybe you'll need to order by the key asc.

cb_Schemes.DataSource = new BindingSource(comboSource, null);
cb_Schemes.ValueMember = "Key";
cb_Schemes.DisplayMember = "Value";
}

Second

public void Populate_SchemesID_Combolists(Dictionary<int, string> comboSource)
{
cb_Schemes.DataSource = new BindingSource(comboSource, null);
cb_Schemes.ValueMember = "Key";
cb_Schemes.DisplayMember = "Value";

cb_Schemes.Text = "";
//or
cb_Schemes.Text = "Select the Scheme";
}

Note: For the two methods you'll need to check if it's selected the correct item before doing anything with the comboBox selected item like:

if (cb_Schemes.SelectedItem.ToString() != "Select the Scheme" || cb_Schemes.SelectedItem.ToString() != "")
{
//do anything
}

I hope you get the idea.

How can I set ComboBox text to a value not in DataSource (C#)?

I could not find any valid way to insert items into a bound DataSource. So instead I ended up doing the following:

BindingSource bindingSource = new BindingSource((System.Collections.Specialized.StringCollection)Properties.Settings.Default.Units, "");
if(!bindingSource.Contains(someSavedValue))
{
bindingSource.Insert(0, someSavedValue));
}
this.cboUnit.DataSource = bindingSource;

This creates a new instance of the binding source that can be modified prior to binding it to a data source. I wrapped this code in a method for which I can pass in a string collection and the stored value (savedUnit in this case) that returns a new BindingSource instance. This is especially useful in my case, as I can now simply write:

this.cboLengthUnit.DataSource = CreateBindingSource(unitsCollection, savedLengthUnit);
this.cboWidthUnit.DataSource = CreateBindingSource(unitsCollection, savedWidthUnit);
this.cboHeightUnit.DataSource = CreateBindingSource(unitsCollection, savedHeightUnit);

I wrote the code above from memory, so it may contain a mistake.

c# combobox set the value using text

Before setting the text farmRegion.Text = myText put a break point and checks the combobox datasource, and ensure myText is present in combobox.

If you handled any events of combobox put a break point on that events and check what happend after the execution of farmRegion.Text = myText statement.

These two steps doen't solve your issue then find out the index of your text value as

int index = farmRegion.FindString(myText);
farmRegion.SelectedIndex = index;


Related Topics



Leave a reply



Submit