如何只从一个组合框中检索选定的数据到另一个组合框中 [英] How to retrieve only selected data from one combo box to another combo box

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

问题描述

我正在创建桌面应用程序,在该应用程序中我创建了一个窗体,其中包含三个组合框,即cboproduct,cborawmaterial_id和cborawmaterial.
我的产品有蒸气熨斗,移动式榨汁机和榨汁机,并带有各自的产品编号.
我有恒温器,嵌体,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

SqlConnection con;
           // SqlCommand com;
           String message;
           con = new SqlConnection(@"Data Source=AKMINDER-PC\SQLEXPRESS;Initial Catalog=elin appliances(Comaker of PHILIPS);Integrated Security=true");
           SqlCommand com = new SqlCommand();
           message = "Select product_id, product_name 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.DisplayMember = "product_name";
               combo1.ValueMember = "product_id";

               //combo1.Items.Insert(0, "--Select--");
           }
           else
           {
               lblMsg.Text = "No product found";
           }

               }
                private void fillrawmaterialid(string proid)

       {
           SqlConnection con;
          // SqlCommand com;
          // String message;
           con=new SqlConnection(@"Data Source=AKMINDER-PC\SQLEXPRESS;Initial Catalog=elin appliances(Comaker of PHILIPS);Integrated Security=true");
           SqlCommand com = new SqlCommand();
           //message = "Select Raw_Material_ID ,Raw_Material from Raw_Info where product_id='"+combo1.SelectedIndex+"'";
           com.Connection = con;
           com.CommandText = "Select Raw_Material_ID ,Raw_Material from Raw_Info where product_id='" + proid + "'";
         com.Parameters.AddWithValue("@proid", proid);

           DataSet DS = new DataSet();
           SqlDataAdapter DA = new SqlDataAdapter();


           DA.SelectCommand = com;
           con.Open();

          // com.ExecuteNonQuery();

           DA.Fill(DS);
           con.Close();
           if (DS.Tables[0].Rows.Count > 0)
           {
               comborawid.DataSource = DS.Tables[0];
               comborawid.DisplayMember = "Raw_Material";
               comborawid.ValueMember = "Raw_Material_ID";

               //combo1.Items.Insert(0, "--Select--");
           }
           else
           {
               lblMsg.Text = "No product found";
           }
private void combo1_SelectedIndexChanged(object sender, EventArgs e)
       {
          string proid=(combo1.SelectedIndex.ToString());
           fillrawmaterialid(proid);


       }


现在,有了valuemember和Display成员,我可以在combo1中获取值,但是我还想要与cborawmaterial中Combo1的选定索引相对应的原材料列表.
例如,如果我在combo1_SelectedIndexChanged中选择产品笔记本电脑,则只有我应该获取与笔记本电脑相关的特定原材料,而不是存储在数据库中的所有原材料
但我正在获取原材料的全部清单

告诉我有关选择查询的信息...需要什么更改?


Now with valuemember and Display member i am getting values in combo1 but i also want the list of raw material corressponding to selected index of Combo1 in cborawmaterial.
e.g if I select Product laptop in combo1_SelectedIndexChanged then only I should get specific raw material related to laptop not all rawmaterials which is stored in database
but i am getting whole list of raw material

do tell me about select query ...what change is need?

推荐答案

Ak

我认为您不需要以下内容:

Hi Ak

I don''t think you need the line:

com.Parameters.AddWithValue("@proid", proid);



由于您已经在字符串中插入了proid值:



Since you are already inserting a value for proid in the string:

com.CommandText = "Select Raw_Material_ID ,Raw_Material from Raw_Info where product_id='" + proid + "'";



选择查询以获取原材料对我来说是正确的.

我在想如何初始化Rawid组合框?因此,最初显示组合框时,它们是否包含任何内容?在填充fillrawmaterialid()之前,可能有必要清除原材料组合的内容.



The select query to get the raw materials looks correct to me.

I was thinking how do you initialise the rawid combo box? So when the combo boxes are initially displayed do they contain anything? It might be worth clearing the contents of the raw material combo before you populate it in fillrawmaterialid().


在我看来,更好的解决方案是将BindingSource与DataRelation一起使用

这里给出一个例子

http://msdn.microsoft.com/en-us/library/c12c1kx4.aspx [ ^ ]

在您的情况下,设置combo1.DataSource = MasterBindingSource和
comborawid.DataSource = DetailsBindingSource

这样,无论何时在combo1中选择了产品,相应的原材料都将在comborawid中显示,并具有数据关系.

如果您的问题得到解决,则可以接受该解决方案并对其进行投票,否则请发表您的疑问.

PES
In my view the better solution is to use BindingSource with DataRelation

An example is given here

http://msdn.microsoft.com/en-us/library/c12c1kx4.aspx[^]

In your case set the combo1.DataSource = MasterBindingSource and
comborawid.DataSource = DetailsBindingSource

This way whenever a product is selected in combo1, the corresponding raw materials will be displayed in comborawid, with the datarelation.

If your problem is solved, you may accept and vote the solution, otherwise please post your queries.

PES


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

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