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";
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 have a ComboBox control.
I bind to this control to the DataSet table.
Here is the code:
comboBox.Items.Add(("Select"));
comboBox.DataSource = DataSet.ColorTable;
comboBox.DisplayMember = DataSet.ColorTable.ColorNameColumn.ColumnName;
comboBox.ValueMember = DataSet.ColorTable.ColorIDColumn.ColumnName;
This result I get:
I want to display on the top of the list SELECT: word. So I need to add addition Item to the comboBox control.
Here how I implement it:
cmbCategory.Items.Add(("Select"));
But the result is still the same as above. I get only colors without SELECT: word on the top of the list.
Any idea how I can add this string-SELECT: to the ComboBox control and set to this string ValueMember?
Use Insert method instead.
cmbCategory.Items.Insert(0, "Select");
Note : Put this code after the databind.
You can add the collections of color to an array or a dataset (if you are getting them from database) first and then add items "select", then add each elements of the array or a column of the dataset.
Do this in Form_Load function and wherever there are changes made in color collections array or database.
//This will set Display member and value member
comboBox.DisplayMember = "ColorName";
comboBox.ValueMember = "ColorCode";
//This will add a new row to table in binded dataset
DataRow dr = dal.MyProperty_dsColors.Tables["ColorInfo"].NewRow();
dr["ColorName"] = "Select Color"; //SomeName
dr["ColorCode"] = 001; //Some ID
dal.MyProperty_dsColors.Tables["ColorInfo].Rows.Add(dr);
//binding dataSource
comboBox.DataSource = dal.MyProperty_dsColors.Tables["ColorInfo"];
What would also help you is that you set the ComboBox without having to 'Select' it when the popup arrives...
Select your ComboBox, under the properties tab, select Appearance->Drop Down Style and select DropDownList.
If we want to add manually values in a combobox (for example integers) this can be done using a for loop:
// sample code
int lower=1;
int higher=500;
for (int i=lower; i<=higher; i++)
combo_values.Items.Add(i.ToString());
Note that you have to use the int.Parse(combo_values.Text) command to read a value.
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;
I have DataGridViewComboBoxCell and a DataTable. The data in Table I bound with DataGridViewComboBoxCell using DataSource and set ValueMember, and DisplayMember.
private void Form1_Load(object sender, EventArgs e)
{
DataGridViewComboBoxCell comboBoxCell = new DataGridViewComboBoxCell();
dataGridView1.Rows[0].Cells[0] = comboBoxCell;
comboBoxCell.DataSource = dataTable;
comboBoxCell.ValueMember = "ID";
comboBoxCell.DisplayMember = "Item";
}
How can I programmatically set the value in the cell when the form loads?
In the simple ComboBox I know a property SelectedIndex.
I tried comboBoxCell.Value = ...; but it gives an exception.
And tried
private void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
e.Value = 1;
}
It sets a new value in the cell, but I need to select a value.
Form loaded and I have empty cell.
And some data in the ComboBox.
When I put this code dataGridView1.Rows[0].Cells["ComboColumn"].Value = "1"; right after comboBoxCell.DisplayMember = ... (see above), it works fine.
The value "1" in the ID column corresponds to the value "Second" in the Items column.So, I get the correct result.
Sorry for my English and my newbie code :)
Instead of adding a cell to your grid add a DataGridViewComboBox column.
DataGridViewComboBoxColumn c = new DataGridViewComboBoxColumn();
c.Name = "ComboColumn";
c.DataSource = dataTable;
c.ValueMember = "ID";
c.DisplayMember = "Item";
dataGridView1.Columns.Add(c);
To select a particular value you set the Value property of a given cell.
dataGridView1.Rows[rowIndexYouWant].Cells["ComboColumn"].Value = 1;
Note that the type here is important! In comments you say you get a System.FormatException. This can be caused by setting the wrong type to the value.
When you set the value to 1 you are assigning an int - if for some reason you have strings in the ID column you will get the System.FormatException exception you are seeing.
If the types differ you need to either update the DataTable definition or set the value to a string:
dataGridView1.Rows[rowIndexYouWant].Cells["ComboColumn"].Value = "1";
Also note that this value must be present in the ID column of the DataTable that you have set as the source of the grid.
It is usually easiest to work with a DataGridView when it has its DataSource set. In this case you can bind the ComboBoxColumn to the grid's DataSource using the DataPropertyName property.
c.DataPropertyName = "GridDataSourceColumnName";
This allows the columns value to be taken from the grid data source and for changes to the column to directly change that data source.
Lastly, do not use the CellFormatting event here - this event is not intended for this sort of use. It is usually best to do this sort of work in the DataBindingComplete event (if you only want it done once) or during some event like DefaultValues needed or RowValidating.
By using CellFormatting you will probably make it impossible for users to manually edit the combo box.