Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Combo Box Items Value
 

Combo Box Items Value

Hi Guys, I wanna have a combo box that returns a value other then its text to its data source. Basically, I have a textbox that has the values 'Male' and 'Female' and I have it bound to a datasource and I want it to return a value of just m or f. Any ideas???

Thanks in Advance

Dancer  Monday, September 25, 2006 11:27 AM

Hi,

if you're databinding your combo, you can use its DisplayMember and ValueMember properties. DisplayMember is the datasource item's field, which will be used displaying in the combobox's text area, and ValueMember will contain the item's value. For example, take a look at this code:

DataTable table = new DataTable();
table.Columns.Add(
"GenderDescription", typeof(string));
table.Columns.Add(
"GenderKey", typeof(string));
table.Rows.Add(
new string[] { "Female", "F" });
table.Rows.Add(
new string[] { "Male", "M" });

comboBox1.ValueMember = "GenderKey";
comboBox1.DisplayMember =
"GenderDescription";
comboBox1.DataSource = table;

The datatable includes two columns: GenderDescription is used as a DisplayMember field, while GenderKey is used for a ValueMember. If you want to retrieve the currently selected value, you can use combo's SelectedValue property:

string gender = comboBox1.SelectedValue as string;

Hope this helps,

Andrej

Andrej Tozon  Monday, September 25, 2006 12:04 PM
Sorry accidentally created this thread twice
Dancer  Monday, September 25, 2006 11:30 AM

Hi,

if you're databinding your combo, you can use its DisplayMember and ValueMember properties. DisplayMember is the datasource item's field, which will be used displaying in the combobox's text area, and ValueMember will contain the item's value. For example, take a look at this code:

DataTable table = new DataTable();
table.Columns.Add(
"GenderDescription", typeof(string));
table.Columns.Add(
"GenderKey", typeof(string));
table.Rows.Add(
new string[] { "Female", "F" });
table.Rows.Add(
new string[] { "Male", "M" });

comboBox1.ValueMember = "GenderKey";
comboBox1.DisplayMember =
"GenderDescription";
comboBox1.DataSource = table;

The datatable includes two columns: GenderDescription is used as a DisplayMember field, while GenderKey is used for a ValueMember. If you want to retrieve the currently selected value, you can use combo's SelectedValue property:

string gender = comboBox1.SelectedValue as string;

Hope this helps,

Andrej

Andrej Tozon  Monday, September 25, 2006 12:04 PM
That does help butI need one more thing. I need the value of the selected item to be bound another field in a database. I have tried doing this through the form designer but when I change the field and then click my update button the text changes to F or M respectively. Any ideas?
Dancer  Wednesday, September 27, 2006 10:38 AM
Ok scratch that last bit. I was just having a play around and i realised that it actualy is working so yeah not sure what was happening there. Thanks very much for your help.
Dancer  Wednesday, September 27, 2006 11:13 AM

You can use google to search for other answers

Custom Search

More Threads

• DataGridViewComboBox blacks out (back color is set to KnownColor.Black) when using EditingControlShowing event
• Image in PictureBox
• DataGridView, PropertyGrid and Attribute "BrowsableAttribute"
• forcing update of data from dataGridView to bound dataSet
• Threading and NewRow
• Can datagridview host a user control (not derived from the commonly used controls)
• Control.Enabled=False - becomes ugly. Why?
• DataGridView refresh
• How can I get data binding to work with a ListBox.SelectedItem
• DataGrid Databinding