本文介绍了如何从数据库到组合框检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建桌面应用程序,在该应用程序中,我创建了一种形式,其中有三个组合框,即cboproduct,cborawmaterial_id和cborawmaterial.
我有产品,即蒸气熨斗,移动式榨汁机和榨汁机,具有相应的产品ID.
我有原材料恒温器,嵌体,ic,杯,罐,led.
我希望当我一次从cboproduct中选择一种产品时,应该在cborawmaterial中获取其各自的原材料.
我创建了2张桌子
带字段product_id和产品
的product_info具有字段product_id,rawmaterial_id和rawmaterial的rawmaterial_info

我尝试过此编码


I am creating desktop application in which i have created a form in which there are three comboboxes namely cboproduct,cborawmaterial_id and cborawmaterial.
I have products namely steamiron,mobile,juicer with respective product id.
I have rawmaterials thermostat,inlay,ic,cuplor,jar,led.
I want that when i select one product at a time from cboproduct,i should get its respective rawmaterials in cborawmaterial.
I created a 2 tables
product_info with field product_id and product
rawmaterial_info with field product_id ,rawmaterial_id and rawmaterial

I tried this coding


private void fillProductId()
        {
            SqlConnection con;
            SqlCommand com;
            String message;
            con=new SqlConnection(@"Data Source=AKMINDER-PC\SQLEXPRESS;Initial Catalog=elin appliances(Comaker of PHILIPS);Integrated Security=true");
            message
            = "Select Product_ID, Product from product_info";
            con.Open();
            DataSet DS = new DataSet();
            SqlDataAdapter DA = new SqlDataAdapter(); 
           
            DA.SelectCommand = new SqlCommand(message, con);
            com.ExecuteNonQuery();
           
            DA.Fill(DS);
            con.Close();
            if (DS.Tables[0].Rows.Count > 0)
            {
               combo1.DataSource = DS.Tables[0];
                combo1.DataTextField = "Product";
                combo1.DataValueField = "Product ID";
                combo1.DataBind();               combo1.Items.Insert(0, "--Select--");
            }
            else
            {
                lblMsg.Text = "No product found";
            }
        }



错误



error

combo1.DataTextField = "Product";
                combo1.DataValueField = "Product ID";
                combo1.DataBind();



不包含DataTextField,DataValueField,DataBind的定义,并且没有扩展方法接受类型的第一个参数(您是否缺少using指令或程序集引用?)



does not contain a definition for DataTextField ,DataValueField,DataBind and no extension method accepting a first argument of type (are you missing a using directive or an assembly reference?)

推荐答案

ComboBox1.DataSource = dsData.Tables("table1");
ComboBox1.DisplayMember = "Display Field name";
ComboBox1.ValueMember = "Value Field Name";



您的情况将是:-



In your case it will be:-

combo1.DataSource = DS.Tables[0];
combo1.DisplayMember = "Product";
combo1.ValueMember = "Product_ID";



该链接将对您有所帮助:- http://www.akadia.com/services/dotnet_databinding.html [^ ]

如果您有帮助,请不要忘记将其标记为答案.

谢谢



This link will be helpful to you:- http://www.akadia.com/services/dotnet_databinding.html[^]

Please don''t forget to mark this as your answer if it helps you out.

Thanks


combobox1.DataSource  = ds;
combobox1.DisplayMember = "EmpName";
combobox1.ValueMember = "EmpId";



这篇关于如何从数据库到组合框检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 08:35