简体   繁体   中英

SelectedValuePath in wpf c# from oracle DataBase

I'm a little confusing using the selectedvaluepath. indNacionalidad still bringing to me a null value and I just want the id of the selected item in the combobox. Load:

private void combonacionalidad_Loaded(object sender, RoutedEventArgs e)
    {
        using (OracleConnection ora = new OracleConnection(con))
        {
            ora.Open();
            OracleCommand comm = new OracleCommand("select * from NACIONALIDAD order by idnacionalidad", ora);
            comm.CommandType = System.Data.CommandType.Text;
            comm.ExecuteNonQuery();

            OracleDataReader dr = comm.ExecuteReader();

            OracleDataAdapter oda = new OracleDataAdapter(comm);
            DataTable dt = new DataTable();
            oda.Fill(dt);
            combonacionalidad.ItemsSource = dt.AsDataView();
            combonacionalidad.DisplayMemberPath = dt.Columns[1].ToString();
            combonacionalidad.SelectedValuePath = "idnacionalidad";
            ora.Close();
        }
    }

SelectionChanged:

private void combonacionalidad_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        String indNacionalidad = ((ComboBoxItem)combonacionalidad.SelectedValue).ToString();
    }

and if its possible, how do you get an int?

XAML:

<ComboBox x:Name="combonacionalidad" HorizontalAlignment="Left" Margin="520,76,0,0" VerticalAlignment="Top" Width="110" Loaded="combonacionalidad_Loaded" SelectionChanged="combonacionalidad_SelectionChanged"/>

It is simple. Just change the SelectionChanged event as follows.

private void combonacionalidad_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int indNacionalidad = combonacionalidad.SelectedIndex;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM