none
how to get selected value from combobox?

    Question

  • i have combobox which populated with data from sql db now when user select value i want to get selected value in combobox

    in this code i get the place of the value in sql table but not value himself can anyone help me??

     

    private

     

    void button1_Click(object sender, RoutedEventArgs e)

    {

     

     

    int selectedIndex = comboBox9.SelectedIndex;

     

    Object selectedItem = comboBox9.SelectedItem;

     

    MessageBox.Show("Selected Item Text: " + selectedItem.ToString() + "\n" +

     

    "Index: " + selectedIndex.ToString());

    }

    i get showbox  "  slected item text system.data.dataRowView"

    Wednesday, August 25, 2010 1:49 PM

Answers

  • Hi Dan,

    I guess the simplest way would be if you created a new project and started from scratch - all the libs required should already be there in the first place. I have created a sample solution (VS 2008) with the stuff I posted in the other thread (I also updated the post there to show the link) which you can download here . Unless you have the Northwind database on your primary SQL Server instance, you will have to change the ConnectionString accordingly (look for _constrConnectionString in the code). That's probably all the help I can offer ...


    Cheers,
    Olaf
    http://blogs.intuidev.com
    • Marked as answer by Dan_1982 Thursday, August 26, 2010 4:21 PM
    Wednesday, August 25, 2010 3:23 PM

All replies

  • You get a  DataRowView because that's what's actually selected. DataRowView.Row should give you the actual Row.
    Wednesday, August 25, 2010 2:01 PM
  • string mySelectedValue = comboBox9.SelectionBoxItem.ToString();
    MessageBox.Show(mySelectedValue);

    My .NET Blog: http://michaelcrump.net
    Wednesday, August 25, 2010 2:02 PM
  • Thanks for answers but now i get showBox " system.Data.DatarowView" but not selected value.

     

     

    private void button1_Click(object sender, RoutedEventArgs e)

    {

     

    string mySelectedValue = comboBox9.SelectionBoxItem.ToString();

     

    MessageBox.Show(mySelectedValue);

    }

     

    Wednesday, August 25, 2010 2:10 PM
  • Hi Dan,

    if you take the sample I posted in your other thread , you would get the ID of your selected item simply via cbo.SelectedValue - this is due to the fact that the ComboBox'es SelectedValuePath property points to the corresponding field in your database, so this would give you the CustomerID for cboCustomers and OrderID for cboOrdersForCustomer.


    Cheers,
    Olaf
    http://blogs.intuidev.com
    Wednesday, August 25, 2010 2:34 PM
  • ok . but why this code not work?

     

    thanks

    Wednesday, August 25, 2010 2:43 PM
  • Hi Dan,

    ok . but why this code not work?

    well, that actually depends on what is behind your cbo's items, and if this is something other than what I posted in the other thread, you'll have to show us what data each item represents resp. how you filled your cbo. Again, if you take my Northwind-sample, you could simply get the selected ID via cboWhateverCombobox.SelectedValue. To get the text behind the selected item OTOH, you could simply do a string strTest=cboWhateverCombobox.Text; . Alternatively, if you followed my example, the DataRowView which is behind each item in the cbo will contain all fields from the query that you used to fill the cbo. As a result, you can reference each of those fields. For instance, if you take the Customer combobox you'd get the value of the CompanyName field via string strTest=((DataRowView)cboCustomers.SelectedItem).Row["CompanyName"]; . That is, you cast the cbo's SelectedItem to a DataRowView (which is what's behind it) and then use the desired column's name (or its index) found in the Row object.

    Cheers,
    Olaf
    http://blogs.intuidev.com
    Wednesday, August 25, 2010 3:04 PM
  • a problem that i still can't debug your code.

     

     

    Wednesday, August 25, 2010 3:10 PM
  • Hi Dan,

    I guess the simplest way would be if you created a new project and started from scratch - all the libs required should already be there in the first place. I have created a sample solution (VS 2008) with the stuff I posted in the other thread (I also updated the post there to show the link) which you can download here . Unless you have the Northwind database on your primary SQL Server instance, you will have to change the ConnectionString accordingly (look for _constrConnectionString in the code). That's probably all the help I can offer ...


    Cheers,
    Olaf
    http://blogs.intuidev.com
    • Marked as answer by Dan_1982 Thursday, August 26, 2010 4:21 PM
    Wednesday, August 25, 2010 3:23 PM