将数据库中的项添加到组合框和文本框中

时间:2016-05-14 07:12:20

标签: c#

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            listParent.Add("Chocolate");
            listParent.Add("Biscuit");
            listParent.Add("Milk");
            listParent.Add("Sugar");
            listParent.Add("Flour");
            listParent.Add("Ice Cream");

            listChild.Add(new Tuple<string, string>("Chocolate", "Kandos"));
            listChild.Add(new Tuple<string, string>("Chocolate", "Edna"));
            listChild.Add(new Tuple<string, string>("Chocolate", "Mars"));
            listChild.Add(new Tuple<string, string>("Chocolate", "Rovello"));

            listChild.Add(new Tuple<string, string>("Biscuit", "Maliban"));
            listChild.Add(new Tuple<string, string>("Biscuit", "Munchee"));
            listChild.Add(new Tuple<string, string>("Biscuit", "Maam"));

            listChild.Add(new Tuple<string, string>("Milk", "Anchor"));
            listChild.Add(new Tuple<string, string>("Milk", "Nespray"));
            listChild.Add(new Tuple<string, string>("Milk", "Raththi"));
            listChild.Add(new Tuple<string, string>("Milk", "Lakspray"));

            listChild.Add(new Tuple<string, string>("Sugar", "Arpico"));
            listChild.Add(new Tuple<string, string>("Sugar", "Cargils"));

            listChild.Add(new Tuple<string, string>("Flour", "Prima"));
            listChild.Add(new Tuple<string, string>("Flour", "MDK"));

            listChild.Add(new Tuple<string, string>("Ice Cream", "Elephant House"));
            listChild.Add(new Tuple<string, string>("Ice Cream", "Keels"));
            listChild.Add(new Tuple<string, string>("Ice Cream", "Highland"));



            foreach (var item in listParent)
            {
                cmbParent.Items.Add(item);
            }
        }

        List<String> listParent = new List<String>();
        List<Tuple<String, String>> listChild = new List<Tuple<String, String>>();

在我的c#程序中,我有两个组合框。一个组合框根据第一个组合框更改其值。第一个组合框显示&#34; 产品类别&#34; &安培;第二个显示&#34; 产品品牌&#34;根据第一个。我已经编写了正确执行此操作的代码。

现在我已经输入了所有&#34; 产品品牌&#34;用他们的&#34; 价格&#34;进入数据库。我想添加一个文本框,显示第二个组合框中当前所选产品的价格。换句话说,当第二个组合框的值更改时, price 文本框也应该更改。

当我从组合框中选择巧克力时,如果我在数据库中输入巧克力的价格为100美元,则文本框值应为100.

帮我写代码。我是c#的新手。

1 个答案:

答案 0 :(得分:0)

你必须在你的组合框中添加事件处理程序。

SelectedItemChanged事件处理程序添加到您的cmbParent组合框中,因此当用户更改产品类别时,您可以过滤listChild并删除其他产品类别。然后,将这些已过滤的项目添加到您的其他组合(cmbChild)。

接下来就是将SelectedItemChanged事件处理程序添加到您的cmbChild组合框中。当用户从该组合中选择某些内容时,将触发事件,您可以连接到数据库并获取价格。

看看实现所述事情的代码。

        public Form1()
        {
            InitializeComponent();


            listParent.Add("Chocolate");
            listParent.Add("Biscuit");
            listParent.Add("Milk");
            listParent.Add("Sugar");
            listParent.Add("Flour");
            listParent.Add("Ice Cream");

            listChild.Add(new Tuple<string, string>("Chocolate", "Kandos"));
            listChild.Add(new Tuple<string, string>("Chocolate", "Edna"));
            listChild.Add(new Tuple<string, string>("Chocolate", "Mars"));
            listChild.Add(new Tuple<string, string>("Chocolate", "Rovello"));

            listChild.Add(new Tuple<string, string>("Biscuit", "Maliban"));
            listChild.Add(new Tuple<string, string>("Biscuit", "Munchee"));
            listChild.Add(new Tuple<string, string>("Biscuit", "Maam"));

            listChild.Add(new Tuple<string, string>("Milk", "Anchor"));
            listChild.Add(new Tuple<string, string>("Milk", "Nespray"));
            listChild.Add(new Tuple<string, string>("Milk", "Raththi"));
            listChild.Add(new Tuple<string, string>("Milk", "Lakspray"));

            listChild.Add(new Tuple<string, string>("Sugar", "Arpico"));
            listChild.Add(new Tuple<string, string>("Sugar", "Cargils"));

            listChild.Add(new Tuple<string, string>("Flour", "Prima"));
            listChild.Add(new Tuple<string, string>("Flour", "MDK"));

            listChild.Add(new Tuple<string, string>("Ice Cream", "Elephant House"));
            listChild.Add(new Tuple<string, string>("Ice Cream", "Keels"));
            listChild.Add(new Tuple<string, string>("Ice Cream", "Highland"));



            foreach (var item in listParent)
            {
                cmbParent.Items.Add(item);
            }

            cmbParent.SelectedIndexChanged += CmbParent_SelectedIndexChanged;
            cmbChild.SelectedIndexChanged += CmbChild_SelectedIndexChanged;
        }

        List<String> listParent = new List<String>();
        List<Tuple<String, String>> listChild = new List<Tuple<String, String>>();


        private void CmbChild_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedBrand = (string)cmbChild.SelectedItem;

            //get data from database
            SqlConnection conn = new SqlConnection("[your connection string]");
            SqlCommand command = new SqlCommand();
            command.Connection = conn;

            command.CommandText = "Select Price from [your table] where productBrand = @productBrand";
            command.Parameters.AddWithValue("@productBrand", selectedBrand);

            conn.Open();
            decimal price = (decimal)command.ExecuteScalar();

            txtPrice.Text = price.ToString();

            conn.Close();
        }

        private void CmbParent_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<Tuple<String, String>> productsList = new List<Tuple<String, String>>();
            productsList = listChild.Where(x => x.Item1 == (string)cmbParent.SelectedItem).ToList();

            cmbChild.Items.Clear();
            foreach (var item in productsList)
            {
                cmbChild.Items.Add(item.Item2);
            };
        }