[英]C# WPF Get Value from first Combobox and change value of second Combobox
For days I have been trying to connect two comboboxes to the mysql database, creating a primary and a secondary combobox, depending on the choice of the first combobox should return precise results in the second, I tried to use "SelectionChanged", but I can get the correct value, only after changing it a second time.几天来,我一直在尝试将两个组合框连接到 mysql 数据库,创建一个主要和一个辅助 combobox,这取决于第一个 combobox 的选择应该返回“精确”,但是,只有在第二次更改后才能获得正确的值。
Premise before creating the post I searched a lot in google, and above all I examined several questions on the site (very old), but I can't solve在创建帖子之前我在谷歌搜索了很多,最重要的是我检查了网站上的几个问题(非常老),但我无法解决
LOADER FIRST COMBOBOX:装载机第一 COMBOBOX:
public void load()
{
MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["dbx"].ConnectionString);
MySqlCommand cmd = new MySqlCommand("SELECT CLASSIFICATION FROM `classification`", conn);
conn.Open();
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
conn.Close();
TipoClass.ItemsSource = dt.DefaultView;
}
LOADER SECOND COMBOBOX:装载机第二 COMBOBOX:
public void loadOne(string name)
{
MessageBox.Show(name);
MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["dbx"].ConnectionString);
MySqlCommand cmd = new MySqlCommand("SELECT DETAILS FROM `details` WHERE CLASSIFICATION = " + name, conn);
conn.Open();
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
conn.Close();
DetailsClass.ItemsSource = dt.DefaultView;
}
XAML.CS: XAML.CS:
private void SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (TipoClass.SelectedItem != null)
{
string myString = TipoClass.Text;
MessageBox.Show(myString);
}
}
XAML: XAML:
<ComboBox HorizontalAlignment="Left" Grid.Row="11" VerticalAlignment="Center" Width="350" Height="30" Margin="106,0,0,0" x:Name="TipoClass" DisplayMemberPath="CLASSIFICATION" SelectionChanged="SelectionChanged"/>
<ComboBox HorizontalAlignment="Left" Grid.Row="12" VerticalAlignment="Center" Width="350" Height="30" Margin="106,0,0,0" x:Name="DetailsClass" DisplayMemberPath="DETTAGLIO"/>
Try to call your method like this in the event handler:尝试在事件处理程序中像这样调用您的方法:
private void SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string selectedValue = TipoClass.SelectedItem as string;
if (!string.IsNullOrEmpty(selectedValue))
loadOne(selectedValue);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.