Forgive me if this is a rather simple question but I can't figure it out.
I have a frontend build in WPF.
It has a combobox with a datasource from a localDB.
The comboBox has a DisplayMember that gets filled at runtime.
I also have a text label and I want its text property to be whatever the displaymember is at runtime. The displaymember value is an object the user selects via the comboBox dropdown list, for example shoes.
comboBox.DataSource = ProductLogic.GetProduct();
comboBox.DisplayMember = "ProductName";
If I call it like this:
label.Text = comboBox.DisplayMember
It gets the value at code-time (which is "ProductName", but I need i the text value to be whatever it is at runtime. For example "Shoes". How would I go about this?
DisplayMember holds the name of the property which value is displayed in combobox (or any other control which supports it). So the value of DisplayMember is not changing. You probably want to use Text property.
label.Text = comboBox.Text
Related
I have a ComboBox in my project which I have assigned a DisplayMember and a ValueMember. I want the ComboBox to display the relevant DisplayMember when a ValueMember value is given to the ComboBox.
Code examples will be appreciated.
Thank you
Just simple;
comboBox1.SelectedValue = "2"; // it will show you respected display member
I have a dataset connected to a bindinglist that is connected to a combobox in win32.
I would like to make "Computer" to be selected as a displaymember in the combobox when I have a private string with value "Computer".
The main issue is that it doesn't work to use SelectedText in the combobox to make "Computer" to be selected but it works properly when I use selectedIndex. But, in order to use selectedIndex, you have to know the row number from table in order to make the text to be selected.
(Not sure because of tags, I am assuming you are using WinForms for your application).
Try the following:
combobox.SelectedValue = "Computer";
I have an editable ComboBox with a validation on the Text property to make sure manually entered info is valid.
EDIT: All I want to do is populate the .Text property with the ValueMember of a selection rather than the DisplayMember
I also have the .Items populated with valid entries having the DisplayMember and ValueMember set.
My DisplayMember is a caption along with the data, and the ValueMember is the data itself.
So Items might be:
(DisplayMember, ValueMember)
"Foo - 1ab2" , "1ab2"
"Bar - 3cd4" , "3cd4"
I had a validation on the text which can also validate manual user input like "5ef6"
The problem I'm having is that if the user selects an item from the combobox it populates the text field with the DisplayMember property (ex: "Foo - 1ab2") which will fail validation.
I have tried to manually set the .Text property with the SelectedValue or the SelectedItem.Value on each of the three relevant combobox events to no avail.
I would like that the .Text of the ComboBox get populated with the .ValueMember of the item when selected rather than the .DisplayMember
EDIT: I cannot validate by trying to extrapolate the value from the caption. I send the Text off to a service to be validated.
void FillMyCombo
{
KeyValuePair<string, string> listValue1 = new KeyValuePair<string, string>("Foo - 1ab2" , "1ab2")
KeyValuePair<string, string> listValue2 = new KeyValuePair<string, string>("Bar - 3cd4" , "3cd4")
myCombo.Items.Add(listValue1);
myCombo.Items.Add(listValue2);
myCombo.DisplayMember = "Key";
myCombo.ValueMember = "Value";
}
...
void myCombo_TextUpdated
{
if(!myValidationService.Validate(myCombo.Text))
{
do error stuff
}
}
The user can manually enter something like "5ef6" which will pass validation.
But when they select an item from the list, rather than manually entering it, the .Text property gets filled with the caption and not the value ... so it will contain "Foo - 1ab2" and that will fail validation.
EDIT: In response to an answer posted: I cannot change the validation method to "infer" the value from the caption. I have no control over that service. All I'm after is the displayed value
EDIT: Say a user selects "Foo - 1ab2" from the dropdown list, I want the text in the box to say "1ab2"
EDIT: I have also tried to set the .Text property in code but I can't seem to make it work in any of the ComboBox events. If anybody can answer how to programmaticly set the .Text property (and make it commit!) on a selection event they will also answer this question.
What about http://nickstips.wordpress.com/2010/11/19/c-datagridviewcomboboxcolumn-displaying-different-values-in-drop-down-list/ -- they change ValueMember and DisplayMember on the fly upon dropdown opening/closing.
Example is for DatagridViewComboBox. A ComboBox has those events, too, no?
Give it a try and downvote if it does not help :)=
** Update **
Another good-looking solution might be ArgumentException when adding ComboBox column to DataGridView with same DataSource, there look at the not-accepted answer.
Simply validate the .SelectedText property of the ComboBox instance. .Text will always contain what the user sees, i.e. the display property's value:
void myCombo_TextUpdated
{
if(!myValidationService.Validate(myCombo.SelectedText))
{
do error stuff
}
}
SelectedText gets or sets the text that is selected in the editable portion of a ComboBox, I think even if SelectedIndex=-1/SelectedValue=Null.
I'm working on a GUI that allows the user to manipulate xml files. I display the xml file in a datagridview organized neatly by columns through xml elements. I allow the user to add columns as an extention on my project. The column gets added to the dataset table, then updated to the datagridveiew that I use to display the xml file in. I've included the ability for the user to add a combobox column to select choices instead of entering them in constantly like.. true or false. However, that is where the problem lies. Saving a normal column was easy. The combobox column is being a pain.
I have a "save combobox column" to have it updated to the xml and a "save" button to save in a destination of the user's choice.
I've done some research and it seems like the combobox class has such a feature to gain access to the selecteditem in the combobox put in by the user.
Where we have:
ComboBox box = new ComboBox();
box.SelectedItem;
I tried applying this to the combobox column class but it does not have such a function. Thus, I cannot figure out how to directly obtain the value of the user's selected item. I tried experimenting with comboboxcell's as well, but that didn't lead me anywhere either. Both those classes I played around with do not have a... "selected item" function and even google does not have a solution for me. =( I've also tried using the cell.value, but it is "null" for some reason. Even when the user selects an item in the box, it doesn't get saved into the cell's value.
TLDR:
My question in short is, how, if possible, do you gain access to the comboboxcolumn cell's selected item? Additionally, how would you then ensure that the item value is saved in the cell?
Thanks in advance. I'm using .NET 3.5 SP1, through Visual Studio 2008 C#.
Sincerely,
tf.rz
The Control in a DataGridView is not a ComboBox, it is a DataGridViewComboBox and has different properties and methods. From MSDN
Unlike the ComboBox control, the DataGridViewComboBoxCell does not have SelectedIndex and SelectedValue properties. Instead, selecting a value from a drop-down list sets the cell Value property.
However, you mentioned that the Cell.Value is null for you. Well there may be another step you are missing according to the following article (How to: Access Objects in a Windows Forms DataGridViewComboBoxCell Drop-Down List).
You must set the DataGridViewComboBoxColumn.ValueMember or DataGridViewComboBoxCell.ValueMember property to the name of a property on your business object. When the user makes a selection, the indicated property of the business object sets the cell Value property.
If we have bound a datagridcomboboxcell with a different DisplayMember and ValueMember, like so:
dgcombocell.DisplayMember = "Name";
dgcombocell.ValueMember = "Id";
dgcombocell.DataSource = dataset1.Tables[0];
Then for getting SelectedText, and SelectedValue, we can write this code:
string SelectedText = Convert.ToString((DataGridView1.Rows[0].Cells["dgcombocell"] as DataGridViewComboBoxCell).FormattedValue.ToString());
int SelectedVal = Convert.ToInt32(DataGridView1.Rows[0].Cells["dgcombocell"].Value);
I hope it solves your problem.
Use this to get or set selected value:
object selectedValue = currentRow.Cells["comboboxColumnName"].Value
Don't forget to set DisplayMember and ValueMember for your DataGridViewComboBoxColumn
This is how it is done
DataGridViewComboBoxCell comboCell = (DataGridViewComboBoxCell)dgv.Rows[0].Cells[1];
MessageBox.Show(""+comboCell.Items.IndexOf(comboCell.Value));
A .Net combox is actually a composite control made up of a textbox and a dropdownlist. Use box.Text to get the currently displayed information.
EDIT: The row or the cell should have a .FindControl() method. You'll need to do something like:
Combobox box = (Combobox)(row.FindControl("[combobox ID]"));
string val = box.Text;
Basically, you're finding the control within its container (row or cell), then casting the control found as a combobox, then accessing its .Text property.
I use this:
private int GetDataGridViewComboBoxCellSelectedIndex(DataGridViewCell d)
{
return ((DataGridViewComboBoxCell)d).Items.IndexOf(d.Value);
}
I am working on a project for my college where I need to bind data from database into the combobox. I need to store the roll no / enrollment no in the "value" field of combobox and name of the student in the "text" property of the combobox. How can I do that?>???
Please reply ASAP....
You will need to set the DataSource of the combobox to your datasource. Then the ValueMember for the Roll No and the DisplayMember for the name of the student.
e.g
cboStudents.DataSource = dataSet1.Tables["Students"];
cboStudents.ValueMember = "RollNumber";
cboStudents.DisplayMember = "StudentName";
The two complex-bound controls you've most likely encountered are the ComboBox and the Listbox. To complex-bind one of these controls, you need to set the DataSource (where values originate), the DisplayMember (the name of the column of data that supplies the visible list items), and the ValueMember (the name of the column of data that supplies the possible control values).
combobox.DataSource = dataTable
combobox.ValueMember = "id"
combobox.DisplayMember = "name"