Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I have a combobox.
C#
cmbgenric.datasource = datatable("tbl")
cmbgenric.Valuemember = "GenricKey"
cmbgenric.displaymember= "GenricName"


No i have 5 records in Combobox. If select 3 from the combobox, i want the corresponding value from the valuemember.

Please Help

Thanks and Regards,
Mohi

[Edit]The following text copied from the non Solution posted by OP [/Edit]

Dear All,

Is there anyone out there who can help me in reading the values from valuemember of combobox.

Regards,
Mohi
Posted
Updated 15-May-12 17:43pm
v3
Comments
VJ Reddy 15-May-12 23:49pm    
Please post your further queries using the Improve Question button available below the question, so that it will be updated and shown under the active tab of quick answers at the top.

Further any query to the solution may please be posted as a comment to that solution so that the member who has posted the solution will get an email and he / she may respond.

If your queries are posted as solutions then members will think that further solutions were given and chances are that those queries may not be noticed.

Thank you :)

You can use the ComboBox.SelectedItem Property [^] to fetch the item that is currently selected in your comnbo box.

Since you're using a datatable, the item itself should be a DataRowView. So you can reference the value using the Item[^] property.

So something like:
C#
if (cmbgenric.SelectedItem != null) {
   ...((DataRowView)cmbgenric.SelectedItem)["GenricKey"]...
}
 
Share this answer
 
Comments
VJ Reddy 15-May-12 23:38pm    
Good answer. 5!
Wendelius 16-May-12 11:40am    
Thanks VJ :)
VJ Reddy 15-May-12 23:41pm    
Query from OP moved from solution to comment.
Can you explain it bit more.
Regards,
Mohi
Maciej Los 16-May-12 11:34am    
Goog answer, 5!
Wendelius 16-May-12 11:40am    
Thanks losmac :)
The answer for the update to question: Is there anyone out there who can help me in reading the values from valuemember of combobox. can be as follows:

The SelectedValue property of ComboBox can be used to obtain the object containing the value of the member of the data source specified by the ValueMember property as explained here
ListControl.SelectedValue [^]. Even though it is written here as Listcontrol, as ComboBox class[^]is derived from ListControl it is available on ComboBox.

So to get the value
C#
string value = cmbgenric.SelectedValue != null ? 
                      cmbgenric.SelectedValue.ToString() : string.Empty;


Or if the data specified by the ValueMember is other than string type say double type then
C#
double value =Convert.ToDouble(cmbgenric.SelectedValue);

Convert.ToDouble returns 0 when the cmbgenric.SelectedValue is null and does not throw exception.
 
Share this answer
 
v4
Comments
Maciej Los 16-May-12 11:33am    
Good answer, my 5!
VJ Reddy 16-May-12 12:32pm    
Thank you, losmac :)
Maciej Los 16-May-12 11:34am    
Good answer, +5+!
Wendelius 16-May-12 11:40am    
Good example!
VJ Reddy 16-May-12 12:32pm    
Thank you, Mika :)
dim a as string
a = cmbgenric.selectedvalue
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900