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.
Related
I've got really weird problem with combobox on my windows form application.
So my combobox is populated using datasource, it displays names of people and it holds their IDs as cmbRequestor.ValueMember.
public BindingSource requestorBindingSource = null;
private const string cmdAssoc = "SELECT * FROM assoc_vw ORDER BY assoc_name";
requestorBindingSource.DataSource = populateDataTable(cmdAssoc);
cmbRequestor.DisplayMember = "assoc_name";
cmbRequestor.ValueMember = "ID";
cmbRequestor.DataSource = requestorBindingSource;
cmbRequestor.SelectedIndex = 0;
It works fine but if there is an instance of people with the same name and I select 2nd name (of the same name) from the combobox, for some reason once I close the combobox it selects the first name even though I selected 2nd name.
So to make sure they hold different values against their names I have created SelectedIndexChanged event.
private void cmbRequestor_SelectedIndexChanged(object sender, EventArgs e)
{
int x = cmbRequestor.SelectedIndex;
string j = cmbRequestor.SelectedValue.ToString();
var y = cmbRequestor.Items[x];
}
When I debug the code and I select 2nd name (of the same name) the ID behind it is 3069. Once I close the combobox and click save to save the form SelectedIndexChanged is triggered again (that should not happen) and it goes to the first person with the same name and its ID is different.
There are no other events on this control and I dont use it anywhere else. It looks like the control gets confused itself if there is an instance of the same name.
Change DropDownStyle property to DropDownList. Default value is DropDown and in that case selected item will be determined by the first matched text in the list. DropDown is mainly used in conjunction with autocomplete logics.
EDIT:
If you have to stick with DropDown style, the best workaround will be to handle DropDownClosed event, at that point you will have the correct index selected.
I found that if I set FormattingEnabled to false in Properties, that it works.
I had also the same problem... The best solution for me was to change the DropDown Style property of the combo Box to DropDownList.
When I needed the DropDown style (e.g. to input new data in the Combo Box) I was changing the property to DropDown in the code... and changing back to DropDownList when finished.
try using ComboBox.SelectionChangeCommitted Event for the combobox and maybe you need to remove the default selected index which is set to zero
conn1 = JdbcConn.getConn();
try
{
conn1.Open();
String sqllogin = "Select *from tbladdpattern ";
var cmd = new MySqlCommand(sqllogin, conn1);//This is sql query execute
var reader = cmd.ExecuteReader();//Execute query
IList<string> listName = new List<string>();
while (reader.Read())
{
listName.Add(reader[1].ToString());
}
// listName = listName.Distinct().ToList();
comboBox1.DataSource = listName.Distinct().ToList();
conn1.Close();//Close DataBase Connection
}
catch (Exception ex)
{
conn1.Close();
LogCreate.WriteLog("Errorn in show all pattern " + ex);
}
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 am populating my datagrid based on a value in the textbox.
One of the field values in my grid view is a combobox.On a particular text box entry it shows me the correct results but wen I give another value in the text box, the combobox increases its number which means If I enter 100, the data is populated correctly in my combobox but for any value that is provided next the number of combobox becomes 2.I don't know why this happens. This is the code on button click. please help
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.HeaderText = "Supplier";
//execute sql data adapter to get supplier values
DataTable dt = obj.SqlDataTable("select name1 from blahblah");
//foreach (DataRow supplier in dt.DataSet.Tables[0].Rows)
//{
// combo.Items.Add(supplier[0]);
//}
//dataGridView1.Columns.Add(combo);
foreach (DataRow row in dt.Rows)
{
combo.Items.Add(row["NAME1"].ToString());
}
dataGridView1.Columns.Add(combo);
You're adding a new combobox on every click... instead check if the comboboxColumn is already added and appropriately modify its contents if it is already present.
To do this check, name your combobox column like this:
combo.Name = "Supplier";
the name property will not show in the UI, but Windows Forms will remember it. Then, you can test if your column is already added with:
if (!dataGridView1.Columns.Contains("Supplier"))
I'm not sure about the textbox, but everytime you click, a new DataGridViewComboBoxColumn is added to the grid. If you click 5 times, you'll see 5 DataGridViewComboBoxColumns with the dropdown being the result of the sql query.
You would need to check if the DataGridViewComboBoxColumns combo was already added:
if (dataGridView1.Columns.IndexOf(combo) < 0)
{
// Add
}
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 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