客户端有一个DataSet: dataSet1, 在dataset1中有一个Table: Products.表Products中的字段为
ProductID    int     --产品编号, 主键
ProductName  string  --产品名称 
UnitPrice    decimal --单价products中填充的数据为
1, 'aaa', 1.1
2, 'bbb', 2.2
3, 'ccc', 3.3
在窗体Form1上面有combBox1(显示Products) 和Label1(显示单价) 两个控件。
comboBox1.DataSource     =  dataSet1;    
comboBox1.DisplayMember  =  "Products.ProductName";   --combobox1显示的为ProductName
comboBox1.ValueMember    =  "Products.ProductID";     我的问题是:
当combBox1下拉框切换产品名称的时候,Label1能同时显示这个产品对应的单价,也就是说:当选择'bbb'产品时,给Label1的Text赋值为2.2, 选择'ccc'产品时,给Label1的Text赋值为3.3.  请问怎么做?  多谢了!!(注:dataSet1.Products已经填充到了客户端, 要求使用客户端dataSet1数据集里的数据完成以上功能!!!)多谢。

解决方案 »

  1.   

    在 combox的selectindex事件里做switch(combox1.SelectIndex)
    {
      case 1:
    label1.Text=combox1.Item[3].ToString();

    类似
    }
      

  2.   

    comboBox1.ValueMember    =  "Products.ProductID";  //为什么不改成 comboBox1.ValueMember    =  "Products.UnitPrice";你的ProductID 还有其他的作用吗.....??    private void comboBox1_TextChanged(object sender, EventArgs e)
        {
          label1.Text = comboBox1.SelectedValue.ToString();
        }
      

  3.   


    this.label1.DataBindings.Add(new Binding("Text", comboBox1.DataSource.Table["Products"], "UnitPrice"));
    comboBox1.DataSource.Table["Products"]这里根据你的情况自己变
      

  4.   

    label1.Text=dataSet1.Products[combox1.SelectIndex][3].ToString();
      

  5.   

    在combBox1的SelectedIndexChanged事件中添加Label1.Text = combBox1.SelectedValue.ToString();然后为combBox1赋值的代码comboBox1.ValueMember    =  "Products.ProductID";    应改为comboBox1.ValueMember    =  "Products.UnitPrice";    这样就可以了
      

  6.   

      private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
        { 
           DataSet dataset = (DataSet)(sender as ComBoBox).DataSource;
           this.Lable1.Text = dataset.Tables["Products"].Rows[comboBox1.SelectedIndex]["UnitPrice"].ToString();
        }
      

  7.   

      DataTable dt = dataSet1.Table[0];  private void Form3_Load(object sender, EventArgs e)
      {
        comboBox1.DataSource    =  dt;    
        comboBox1.DisplayMember  =  "Products.ProductName";  --combobox1显示的为ProductName 
        comboBox1.ValueMember    =  "Products.ProductID";    
        flag = true;
       }
        bool flag = false;
        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
          if (flag)
          {
            DataRow[] dr = dt.Select("ID = '"+comboBox1.SelectedValue.ToString()+"'");
            label1.Text = dr[0]["UnitPrice"].ToString();
          }
        }//看看 实现了你要的功能
      

  8.   

    DataTable dt = dataSet1.Table[0];  private void Form3_Load(object sender, EventArgs e)
      {
        comboBox1.DataSource    =  dt;    
        comboBox1.DisplayMember  =  "Products.ProductName";  --combobox1显示的为ProductName 
        comboBox1.ValueMember    =  "Products.ProductID";    
        flag = true;
       }
        bool flag = false;
        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
          if (flag)
          {
            DataRow[] dr = dt.Select("ProductID = '"+comboBox1.SelectedValue.ToString()+"'");  //改下
            label1.Text = dr[0]["UnitPrice"].ToString();
          }
        }//看看 实现了你要的功能
      

  9.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication50
    {
        public partial class Form1 : Form
        {
            ComboBox comboBox1 = new ComboBox();
            Label Label1 = new Label();        public Form1()
            {
                InitializeComponent();            DataSet dataSet1 = new DataSet();
                dataSet1.Tables.Add("products");            DataTable DT = dataSet1.Tables["products"];
                DT.Columns.Add("ProductID", typeof(int));
                DT.Columns.Add("ProductName");
                DT.Columns.Add("UnitPrice", typeof(decimal));
                DT.Rows.Add(new object[] { 1, "aaa", 1.1 });
                DT.Rows.Add(new object[] { 2, "bbb", 2.2 });
                DT.Rows.Add(new object[] { 3, "ccc", 3.3 });            comboBox1.Parent = this;
                comboBox1.DataSource = dataSet1;
                comboBox1.DisplayMember = "Products.ProductName";
                comboBox1.ValueMember = "Products.ProductID";
                comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);            Label1.Parent = this;
                Label1.Location = new Point(0, 100);
            }        void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                Label1.Text = comboBox1.Text;
            }
        }
    }
      

  10.   

    设置 comboBox1 的 Tag 就行了。        private void Form1_Load(object sender, EventArgs e)
            {
                DataTable dtProducts=new DataTable("Products");     //初始化数据
                dtProducts.Columns.Add("ProductID");
                dtProducts.Columns.Add("ProductName");
                dtProducts.Columns.Add("UnitPrice"); 
                dtProducts.Rows.Add(1, "aaa", 1.1);
                dtProducts.Rows.Add(2, "bbb", 2.2);
                dtProducts.Rows.Add(3, "ccc", 3.3 );            this.comboBox1.Tag = dtProducts;
                this.comboBox1.Items.Add("1");
                this.comboBox1.Items.Add("2");
                this.comboBox1.Items.Add("3");
            }        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                DataRow dr = ((DataTable)this.comboBox1.Tag).Select("ProductID=" + this.comboBox1.Text)[0];            this.label1.Text = dr["ProductName"].ToString();
                this.label2.Text = dr["UnitPrice"].ToString();
            }
      

  11.   

    上面的 comboBox1 内容是 ProductID , 如果是 ProductName , 则为:        private void Form1_Load(object sender, EventArgs e)
            {
                DataTable dtProducts=new DataTable("Products");     //初始化数据
                dtProducts.Columns.Add("ProductID");
                dtProducts.Columns.Add("ProductName");
                dtProducts.Columns.Add("UnitPrice"); 
                dtProducts.Rows.Add(1, "aaa", 1.1);
                dtProducts.Rows.Add(2, "bbb", 2.2);
                dtProducts.Rows.Add(3, "ccc", 3.3 );            this.comboBox1.Tag = dtProducts;                   //设置Tap
                this.comboBox1.Items.Add("aaa");
                this.comboBox1.Items.Add("bbb");
                this.comboBox1.Items.Add("ccc");
            }        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                DataRow dr = ((DataTable)this.comboBox1.Tag).Select("ProductName='" + this.comboBox1.Text + "'")[0];            this.label1.Text = dr["UnitPrice"].ToString();
            }
      

  12.   

    DataTable dt = new DataTable();
                dt.Columns.Add("ProductID");
                dt.Columns.Add("ProductName");
                dt.Columns.Add("UnitPrice");            DataRow dr = dt.NewRow();
                dr["ProductID"] = 1;
                dr["ProductName"] = "aaa";
                dr["UnitPrice"] = 1.1;
                dt.Rows.Add(dr);
                DataRow dr1 = dt.NewRow();
                dr1["ProductID"] = 2;
                dr1["ProductName"] = "bbb";
                dr1["UnitPrice"] = 2.2;
                dt.Rows.Add(dr1);
                DataRow dr2 = dt.NewRow();
                dr2["ProductID"] = 3;
                dr2["ProductName"] = "ccc";
                dr2["UnitPrice"] = 3.3;
                dt.Rows.Add(dr2);            DataView dv = dt.DefaultView;
                dv.Sort = "ProductID ASC";
                DataRowView[] drv = dv.FindRows(1);            float f = float.Parse(drv[0].Row.ItemArray[2].ToString());
      

  13.   

    在DataSet中根据ProductID查找UnitPrice
      

  14.   

    首先通过comboBox1.DataSource    =  dataSet1;    
    comboBox1.DisplayMember  =  "Products.ProductName";  
    comboBox1.ValueMember    =  "Products.ProductID";   然后在  private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            { //在这里通过调用一个方法(依据选定的ID得到对应的价格)并附值就行了
               this.label1.Text= lisa.getprice(comboBox1.SelectedValue.ToString());
            }