2

I am working on C#.net windows application. i am filling combobox on my winform by using follows.

cmbEMPType.DataSource = objEntityManager.EmployeeTypes(); cmbEMPType.DisplayMember = "EMPTypeName"; cmbEMPType.ValueMember = "EMPTypeId"; 

where objEntityManager.EmployeeTypes(); in the manager method that gets the List from Linq to sql server. this is working fine.

but as i select the item form combo box, and clicked the button then in the button click event i am getting cmbEMPType.SelectedValue as EmpType return type rather than its Id. why should this? I don't want to create one more EmpType object. need simple selected value. also can not keep faith with SelectedIndex. it may varies for item each time.

**Edited** public List<EMPType> EmployeeTypes() { List<EMPType> EMPTypeList = null; try { if (CommonDataObject.dataContext.EMPAllTypes.Any()) { EMPTypeList = CommonDataObject.dataContext.EMPAllTypes.ToList(); } return EMPTypeList; } catch { return EMPTypeList; } } 

Edited

 private void btnSave_Click(object sender, EventArgs e) { iEMPTypeId = cmbEMPType.SelectedValue; } 

here I must get inte. but asking of create the EMPType object.

4
  • Are you sure you're not using cmbEMPType.SelectedItem? This would normally get the selected object (EmpType), while cmbEMPType.SelectedValue would get the property specified in 'ValueMember'. Commented Feb 14, 2011 at 12:59
  • Yes,i am sure. I checked it thrice. but unfortunately there is SelectedValue. in fact you can also check its return type . it showing 'object'. Commented Feb 14, 2011 at 13:15
  • it will always return object because the value can be of any type so you need to cast it anyway. Please post more code as I asked in my other comment and we'll see what's going on. Commented Feb 14, 2011 at 13:21
  • what do you mean by "asking of create the EMPType object"? Do you get error? If so post here the full error message.. Commented Feb 14, 2011 at 14:02

3 Answers 3

1

This is the correct and expected behavior, you can't change it.

SelectedValue should return the type of the property, e.g. if EMPTypeId is integer it should return integer - please post more code so that we can try figuring out why you get different return value.

If by any chance you're using SelectedItem then have such code to get the ID:

 int selectedID = (cmbEMPType.SelectedItem as EmpType).EMPTypeId; 

To handle cases when there's nothing selected:

object oSelectedEmp = cmbEMPType.SelectedItem; int selectedID = oSelectedEmp == null ? -1 : (oSelectedEmp as EmpType).EMPTypeId; 
Sign up to request clarification or add additional context in comments.

7 Comments

Actually you are incorrect. SelectedValue should give the value of the field specified in ValueMember, NEVER the actual object. If no ValueMember is specified it gives a ToString() representation.
@Rewinder: so how can i figure out this issue. @Shadow Wizard : I think this is enough code i have provided.what you expect? in fact I want to get Selected Value as an integer. but it is giving me object type of EmpType. why should this ?
@Lal post the header of objEntityManager.EmployeeTypes method. Post the way you're reading the selected value.
@Lal not good.. you're calling EmployeeTypes but posted code for EMPTypes - please post code of the relevant method plus how you read the selected value i.e. your button click code.
Oh, sorry my mistake. please review now
|
1

The problem is the sequence of your code. Please remove the first line code to the last line. You will get an int value (iEMPTypeId) from cmbEMPType.SelectedValue.

cmbEMPType.DisplayMember = "EMPTypeName"; cmbEMPType.ValueMember = "EMPTypeId"; cmbEMPType.DataSource = objEntityManager.EmployeeTypes(); iEMPTypeId = cmbEMPType.SelectedValue 

Comments

0

Another option is to override the toString function in your EMPType class. As Edwin de Koning stated "If no ValueMember is specified it gives a ToString() representation."

Something like (I cant test it at the moment):

public override string ToString() { return this.ID; } 

You can check out this article: http://msdn.microsoft.com/en-us/library/ms173154(v=vs.80).aspx

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.