使用c#在windows窗体中的级联组合框中更改第二个组合框选择的填充文本框 [英] fill textbox on second combobox selection changed in cascading combobox in windows form using c#

查看:219
本文介绍了使用c#在windows窗体中的级联组合框中更改第二个组合框选择的填充文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows窗体应用程序中有2个级联组合框。我有价格和单位的文本框。当我选择第一个组合框时,第二个组合框就会被填充。我希望文本框的价格和单位只在第二个组合框选择中填充。我的问题是当表单加载时,两个文本框都填充了表中的值而不是更改组合框选择。



我的代码是:

I have 2 cascading combo-box in windows form application. I have textbox for price and unit. when I select first combobox, second combobox gets populated. I want textbox for price and unit to be filled only on second combobox selection. My problem is when the form is loaded both textboxes are filled with values from table and not on combobox selection changed.

my code is:

private void Purchase_Load(object sender, EventArgs e)
    {

        // TODO: This line of code loads data into the 'supplierDataSet.Supplier' table. You can move, or remove it, as needed.
        this.supplierTableAdapter.Fill(this.supplierDataSet.Supplier);
        fillName();

        comboBoxName.SelectedIndex = -1;

    }



   private void fillName()
   {
       string str = "Select distinct Item_Name from Item";
       using (SqlConnection con = new SqlConnection(@"Data Source=ashish-pc\;Initial Catalog=HMS;Integrated Security=True"))
       {
           using (SqlCommand cmd = new SqlCommand(str, con))
           {
               using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
               {
                   DataTable dtItem = new DataTable();
                   adp.Fill(dtItem);

                    comboBoxName.DataSource = dtItem;
                   comboBoxName.DisplayMember = "Item_Name";
                   comboBoxName.ValueMember = "Item_Name";

               }
           }
       }
   }
   private void fillMake()
   {
       string str = "Select Item_Make from Item Where Item_Name=@Item_Name";
        using (SqlConnection con = new SqlConnection(@"Data Source=ashish-pc\;Initial Catalog=HMS;Integrated Security=True"))
        {

            using (SqlCommand cmd = new SqlCommand(str, con))
            {
                cmd.Parameters.AddWithValue("@Item_Name", comboBoxName.Text);
                using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                {
                    DataTable dtItem = new DataTable();
                    adp.Fill(dtItem);
                    comboBoxMake.DataSource = dtItem;
                    comboBoxMake.ValueMember = "Item_Make";
                    comboBoxMake.DisplayMember = "Item_Make";


                }
            }
        }

   }

  private void comboBoxName_SelectedIndexChanged_1(object sender, EventArgs e)
  {
      if (!string.IsNullOrEmpty(comboBoxName.Text))
      {
          comboBoxMake.Enabled = true;
          fillMake();
          comboBoxMake.SelectedIndex = -1;
      }
  }

  private void comboBoxMake_SelectedIndexChanged_1(object sender, EventArgs e)
  {
      if (!string.IsNullOrEmpty(comboBoxMake.Text))
      {
          textBoxPrice.Enabled = true;
          textBoxUoM.Enabled = true;

      }

      SqlConnection con = new SqlConnection(@"Data Source=ashish-pc\;Initial Catalog=HMS;Integrated Security=True");
      SqlCommand cmd = new SqlCommand("Select * from Item Where Item_Make='" + comboBoxMake.Text + "' AND Item_Name='" + comboBoxName.Text + "'", con);
      SqlDataReader reader;
      try
      {
          if (con.State == ConnectionState.Closed)
          {
              con.Open();
          }
          reader = cmd.ExecuteReader();
          while (reader.Read())
          {
              textBoxPrice.Text = Convert.ToString(reader["Price"]);
              textBoxUoM.Text = Convert.ToString(reader["Unit"]);
          }
      }
      catch (Exception ex)
      {
          MessageBox.Show(ex.Message);
      }
      finally
      {
          if (con.State == ConnectionState.Open)
          {
              con.Close();
          }
      }
  }



我被困住了。请帮助


I am stuck. please help

推荐答案

这个 [ ^ ]可以帮助您。


这篇关于使用c#在windows窗体中的级联组合框中更改第二个组合框选择的填充文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆