简体   繁体   English

将项目从数据库添加到组合框和文本框

[英]Adding items from a database to a combo box and a text box

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>>();

In my c# program I have two combo boxes. 在我的C#程序中,我有两个组合框。 One combo box changes its value according to the first combo box. 一个组合框会根据第一个组合框更改其值。 The first combo box shows " product category " & the second one shows " product brand " according to the first one. 第一个组合框显示“ 产品类别 ”,第二个组合框显示第一个组合的“ 产品品牌 ”。 I have already written the code that does this correctly. 我已经编写了正确执行此操作的代码。

Now I have entered all the " product brands " with their " prices " in to a database. 现在,我已将所有“ 产品品牌 ”及其“ 价格 ”输入数据库。 I want to add a text box that shows the price of the currently selected product from the second combo box. 我想添加一个文本框,以显示第二个组合框中当前所选产品的价格。 In other words, when the value of the second combo box changes the price textbox should also change. 换句话说,当第二个组合框的值更改时, 价格文本框也应更改。

If I have entered the price of chocolate in database as $100 when I choose chocolate from combo box, the text box value should be 100. 从组合框选择巧克力时,如果我在数据库中输入的巧克力价格为100美元,则文本框的值应为100。

Help me to write the code. 帮我写代码。 I am new to c#. 我是C#的新手。

you have to add event handler to your comboboxes. 您必须将事件处理程序添加到组合框。

Add SelectedItemChanged event hander to your cmbParent combobox, so when user changes product category, you can filter listChild and remove other product categories. SelectedItemChanged事件处理程序添加到cmbParent组合框,以便当用户更改产品类别时,可以筛选listChild并删除其他产品类别。 Then, add those filtered items to your other combo ( cmbChild ). 然后,将那些过滤的项目添加到您的其他组合( cmbChild )。

Next thing is to add SelectedItemChanged event hander to your cmbChild combobox. 接下来的事情是将SelectedItemChanged事件处理程序添加到cmbChild组合框。 When user selects something from that combo, event will fire and you can connect to database and fetch prices. 当用户从该组合中选择某项时,事件将触发,您可以连接到数据库并获取价格。

Take a look at code that implements said things. 看一下实现所说事物的代码。

        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);
            };
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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