C3 comboBox different display and value members but need to access both - c#

I have a dataset that has multiple columns which include a Text value to display and a numeric value that I need to use for filtering an another combobox.
MyComboBox.DisplayMember = "Reason";
MyComboBox.ValueMember = "ReasonID";
MyComboBox.DataSource = MyDataTable;
The issue I have is that part of the code I need the ID however for another part of the code I need the text. I can get the ID back but I'm not sure how to access the text when the value changes. I've tried the following
String test1 = MyComboBox.SelectedValue.ToString();
String test2 = MyComboBox.SelectedText.ToString();
Test1 is the ID as I expected. However test2 is "" and I can't see any properties that give the display value instead of the selected value.

Use ComboBox.Text Property
string value = MyComboBox.Text;
Text property contains value of DisplayMember of selected item in your case.
About ComboBox.SelectedText from MSDN
Gets or sets the text that is selected in the editable portion of a
ComboBox.
So this is not a text of selected item

Related

Combobox to sync selected value

The following code is populating my combo box with values
List<Filename> fnList = new List<Filename>();
fnList.Add(new Filename("test1.png"));
fnList.Add(new Filename("test2.png"));
fnList.Add(new Filename("test3.jpg"));
comboBox1.DataSource = fnList;
comboBox1.DisplayMember = "Name";
Now the code below is supposed to sync selected value with the member in associated object.
comboBox1.DataBindings.Clear();
comboBox1.DataBindings.Add("SelectedValue", copy, "EventPicture");
copy is of some class, where EventPicture is string Property. This is where I want to have selected value synced every time user change combo box selection AND every time EventPicture value is changed from other parts of code.
At this point I'm getting an error:
Cannot set the SelectedValue in a ListControl with an empty ValueMember.
You need to set the ValueMember on the ComboBox as well.
comboBox1.ValueMember = "Name";

DataSource, bind more then 1 value

I want to bind more then 1 columns to drop down list, so that I can get the column values when user clicks a button,
ddlListMine.DataSource = GetSomeChickens();
ddListMine.DataTextField = "ChickenName";
ddListMine.DataValueField= "NumberOfEggsChickenLay";
ddListMine.Items.Insert(0, new ListItem("Please Please Please Select....", "0"));
ddListMine.DataBind();
I have another column "ChickenType", which I want to access in Selected Index change column.
GetSomeChickens(); returns 6 columns, including ChickenName, NumberOfEggsChickenLay, ChickenType and so on...
Edit
Off course, I can call database again in selected index change method, but there must be a way around i think
The DropDownList doesn't hold the entire object during the binding, only the Text and Value as defined by DataTextField and DataValueField.
In order to get a selected object back, you can have a method to get the ChickenType by passing the ChickenName using Linq like this.
List<Chicken> Chickens = GetSomeChickens();
Var Chicken= Chickens.FirstOrDefault(c => c.ChickenName== ddlListMine.SelectedItem.Text);
if(Chicken!= null)
{
string ChickenType = Chicken.ChickenType ;
}

How to retrieve all datas from Listbox when selected item changed

I gave datatable as datasource to Lisbox.
That tables fields are ID, Subject, Texts and ID is an Unique field(PK).
But Subject shown as DisplayMember.
Here is my datasource giving code:
lbTexts.DataSource = mDataSet.Tables["Story"];
lbTexts.DisplayMember = "Subject";
i want to know which rows the user has selected and what is the Unique value.
i find it with 10 rows of code. But i hope there is a simple way to find it.
If you use the ValueMember property and set it to Id, you can use the SelectedValue parameter without needing the entire dataset. You're also guaranteed to not get resorting errors where the selected index in the list doesn't match the index in the data.
lbTexts.ValueMember = "Id";
// Later
int selectedId = Int32.Parse(lbTexts.SelectedValue);
Use SelectedItem to get the currently selected item, and use SelectedValue to get the value.
Example of selectedItem:
String value = lbTexts.SelectedItem.Value;
Example of selectedValue:
String txt = lbTexts.SelectedValue.ToString();
Try this,
string value = lbTexts.SelectedItem.Value;

Assign some data fields to DropDownList item

I want to assign 2 data fields to a DropDownList item because I want to display 2 values at click at an item in different elements (for example, 2 textboxes).
For example: at click on a DDL item, that a value of data field named "example" displayed in one TXTBOX and other of a data field named "definition" displayed in other TXTBOX.
If you have do something like this, you can just separate the two values with some sort of delimiter (, or | or whatever), and then parse them out when it's selected and you have to display them.
Item.Value = "VALUE1,VALUE2";
string[] Values = Item.Value.Split(',');
txt1.Text = Values[0];
txt2.Text = Values[1];

Remove Default Selection from a DataBound ComboBox

I have data bind a combo box with a list of values in the database, now, the first record is being displayed as a default value, i need to change this and set to blank or my custom message, any solution?
You can also set the Text property of the ComboBox directly to get a custom message, such as:
comboBox1.SelectedIndex = -1;
comboBox1.Text = "Select an item";
Adding an empty item as the first item to the DataSource before binding is the ugly fix for this issue.
Setting the SelectedIndex as -1 might help.
Below is another option, but you may have to validate when retrieving the selected item.
comboBox.Text = string.Empty;
After binding data to your combobox, insert new item at index 0:
combobox1.Items.Insert(0, "Default Value");
or
combobox1.Items.Insert(0, ""); //Empty

Categories