如何从数据库到组合框检索数据 [英] How to retrieve data from database to combobox

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

问题描述

我正在创建桌面应用程序,在该应用程序中,我创建了一种形式,其中有三个组合框,即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?)

推荐答案

尝试以下操作:-

try this:-

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


您好,

在桌面应用程序中,组合没有DataTextField和DataValueField.

尝试使用以下代码:

Hi,

in desktop application, combo not have DataTextField and DataValueField.

Try using below code:

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


尝试:-

combo1.DataSource = DS;
combo1.DataTextField =产品";
combo1.DataValueField ="Product_ID";
combo1.DataBind();

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

谢谢
try this:-

combo1.DataSource = DS;
combo1.DataTextField= "Product";
combo1.DataValueField= "Product_ID";
combo1.DataBind();

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

Thanks


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

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