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
Related
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
I have a combo box which I want the default value to be -- Select Gender --
I have tried the following coding, and I got this error
Items collection must be empty before using ItemsSource.
Please help
cboGender.ItemsSource = null;
cboGender.DisplayMemberPath = "Display";
cboGender.SelectedValuePath = "Value";
cboGender.SetBinding(ComboBox.ItemsSourceProperty, oBinding);
cboGender.Items.Insert(0, "--Select Gender--");
cboGender.SelectedIndex = 0;
Add "--Select Gender--" to your Collection before binding it to the ItemsSource (if you want it as first, remember to use Insert(0, ) and then after binding it, set the SelectedIndex to it.
May be you should try to call
cboGender.Items.Clear();
before the insertion of the default item.
OR
Just create your data source with the default value item before inserting any value in the combo box and write something like this:
cboGender.Items.Clear();
cboGender.ItemsSource = dataSource;
where dataSource would be the list with the all items.
You can use
<ComboBox x:Name="cboGender"
Text="--Select Gender--" />
if you don't need to choose this default value again after first selection
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 ;
}
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;
We have customar table contains Cust_ID, Cust_Name etc.....
for this table Cust_Name is not unique and one Customer name can repeat Number of times.
i am getting data from SQL and binding to ComboBox (winform)
cmbCustomar.Datasource = GetCustomerData(_LocationID);
cmbCustomar.DisplayMember = "Cust_Name";
cmbCustomar.ValueMember = "Cust_ID";
Here the Problem is :
Customer Name : JOHN is repeated 4 times, all Cust_ID are different
when user select JOHN on first Item i am getting correct "SelectedValue"
but if user select 2 nd or 3rd JOHN
Combobox Item it allways default select First Item (Name as JOHN)
and the SelectedValue allways return the First Item Value.
i am not able to find where i am doing wrong, please suggest.
Try changing the following property:
cmbCustomar.DropDownStyle = DropDownList;
If your ComboBox has DropDownStyle = DropDown, then the "text" part of the ComboBox is trying to match the first item it can find in the list, and in this case, it ignores the current selected item and finds the first "John" on your list.
Keep in mind "SelectedValueChanged" event will fire when combobox being populated. Make sure to un-subscribe to this event before populating the combobox. and subscribe again after populating the data.
//unsubsribe the event before populating combobox1
this.cmbCustomar.SelectedValueChanged -= new System.EventHandler(this.cmbCustomar_SelectedValueChanged);
cmbCustomar.Datasource = GetCustomerData(_LocationID);
cmbCustomar.DisplayMember = "Cust_Name";
cmbCustomar.ValueMember = "Cust_ID";
//subscribe the event again
this.cmbCustomar.SelectedValueChanged += new System.EventHandler(this.cmbCustomar_SelectedValueChanged);
It seems that you are very new to it. And as by my guess, because of your incomplete description, you are trying return the "Selected Value" based on the selected text of the combobox. But rather than that you must try to attach a value a to selected text and return that value. It will surely solve your problem.
Hope it helps.