locked
Retrieving the Value from a Combo Box RRS feed

  • Question

  • I am having a problem retrieving the value from a combo box. I want to take the value, and convert it to an int.

     

    I tried this:

    Convert.ToInt32(cboDept.SelectedItem)

     

    But it doesn't work, since the cboDept is bound to the sorted DefaultView of a  DataTable, like so:

     

    cboDept.ItemsSource = _sortedDeptTable.DefaultView;

    cboDept.DisplayMemberPath = "DeptID";

     

    I have tried everything I can think of, but I cannot get the selected value.

     

    How would I get the value I am looking for?

     

    Thanks

     

     

    Thursday, April 5, 2007 7:46 PM

Answers

  • set the selectedvaluepath property to deptid and then you could you just SelectedValue
    Thursday, April 5, 2007 7:47 PM
  • In this case, you don't need to use the Convert.ToInt32. Simply use:

    int id = (int)cboDept.SelectedValue;

    Friday, April 6, 2007 6:48 AM

All replies

  • set the selectedvaluepath property to deptid and then you could you just SelectedValue
    Thursday, April 5, 2007 7:47 PM
  • Thanks, but it didn't work.

     

    I received the following error:

     

    Unable to cast object of type 'System.Data.DataRowView' to type 'System.IConvertible'.

    Thursday, April 5, 2007 8:16 PM
  • In this case, you don't need to use the Convert.ToInt32. Simply use:

    int id = (int)cboDept.SelectedValue;

    Friday, April 6, 2007 6:48 AM
  • I got the same error, but its not able to convert  selected value, becoz first of all the data which i want to bind, its not binding to the control , before binding the control of execution is flowing into the selectindexchanged , where iam converting the selected value

    so its throwing that exception 

    Thursday, May 10, 2007 8:22 AM
  • NarenR said:

    I got the same error, but its not able to convert  selected value, becoz first of all the data which i want to bind, its not binding to the control , before binding the control of execution is flowing into the selectindexchanged , where iam converting the selected value

    so its throwing that exception 



    try this...it did he lp me resolve the problem..
    databing the parent list control in the following order
    list.displaymemeber
    list.valuemember
    list.datasource
    • Proposed as answer by Sujith Mys Thursday, August 14, 2008 4:09 AM
    • Edited by Sujith Mys Thursday, August 14, 2008 4:23 AM solve the issue
    Thursday, August 14, 2008 4:08 AM