Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have made a WPF Autocomplete Combobox, and the problem which i am facing is that i am only able to get SelectedValue of ComboBox when i press Down Key.

Problem is in below BindComboBox Method, and comboBox5.SelectedValue only works when down Key is pressd.

 public void BindComboBox3(ComboBox comboBox3) //BatchCombobox
    {
        SqlDataAdapter da = new SqlDataAdapter("Select id,batch_id FROM batch where product_id_fk='" + Convert.ToInt32(comboBox5.SelectedValue) + "' and left_qty>0", con);
        DataSet ds = new DataSet();
        da.Fill(ds, "batch");
        comboBox3.ItemsSource = ds.Tables[0].DefaultView;
        comboBox3.DisplayMemberPath = ds.Tables[0].Columns["batch_id"].ToString();
        comboBox3.SelectedValuePath = ds.Tables[0].Columns["id"].ToString();
    }

But in Below code comboBox5.SelectedValue is properly Working.

private void comboBox5_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        comboBox3.IsEnabled = true;
        BindComboBox3(comboBox3); //Problem is in this Function

        con.Open();
        SqlCommand cmd1 = new SqlCommand("SELECT med_type,fk_com_id FROM products where p_id_pk='" + Convert.ToInt32(comboBox5.SelectedValue) + "'", con);

        SqlDataReader med_oftype = null;

        med_oftype = cmd1.ExecuteReader();

        while (med_oftype.Read())
        {
            get_med_type = med_oftype["med_type"].ToString();
            cmpny_id = Convert.ToInt32(med_oftype["fk_com_id"]);
        }

        con.Close(); 

    }

private void ProductsComboBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (((ComboBox)sender).Text != "")
        {
            SqlDataAdapter da = new SqlDataAdapter("Select p_id_pk,p_name FROM products", con);
            DataSet ds = new DataSet();
            da.Fill(ds, "products");
            comboBox5.ItemsSource = ds.Tables[0].DefaultView.RowFilter = "p_name like '" + ((ComboBox)sender).Text + "'+'%'";
            comboBox5.ItemsSource = ds.Tables[0].DefaultView;
            comboBox5.DisplayMemberPath = ds.Tables[0].Columns["p_name"].ToString();
            comboBox5.SelectedValuePath = ds.Tables[0].Columns["p_id_pk"].ToString();
        }
        else
        {
            comboBox5.ItemsSource = null;
        }
    } 

Summary is that in one event handler comboBox5.SelectedValue is working and in other comboBox5.SelectedValue is not working. Dont know what is causing this problem.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
221 views
Welcome To Ask or Share your Answers For Others

1 Answer

  • if you can share your .xaml code this would be helpful
  • also if you can clarify why you send (comboBox3) as a parameter although it is accessible any where in the view
  • as a quick solution you can pass SelectedValue as a parameter to BindComboBox3 method

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...