客户端有一个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数据集里的数据完成以上功能!!!)多谢。
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数据集里的数据完成以上功能!!!)多谢。
解决方案 »
- .net与SQL实现任务提醒
- 请问一下winform下用DataBindings绑定时间的问题
- 高分求教高手们为什么这样调用后台参数不行
- C# 里面做提醒功能,,除了timer控件还有什么办法??哪种方法好点
- javascript调用函数中变量的问题
- btnDeleteChannel.Attributes.Add("onclick", "return confirm('確認刪除嗎?')");为什么第一次不执行!
- 打包的问题多多,,,顶者有粪。
- 大虾们,请问C#如何做屏幕录象机!
- 但我要取出得时候怎么把它还原出来呀??????
- C#中怎么调用dll中的枚举类型参数
- <怎么打WINFORM中实现 点击按钮 弹出网页 来显示我TEXTBOX的HTML>
- C#读取XML问题
{
case 1:
label1.Text=combox1.Item[3].ToString();
类似
}
{
label1.Text = comboBox1.SelectedValue.ToString();
}
this.label1.DataBindings.Add(new Binding("Text", comboBox1.DataSource.Table["Products"], "UnitPrice"));
comboBox1.DataSource.Table["Products"]这里根据你的情况自己变
{
DataSet dataset = (DataSet)(sender as ComBoBox).DataSource;
this.Lable1.Text = dataset.Tables["Products"].Rows[comboBox1.SelectedIndex]["UnitPrice"].ToString();
}
{
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();
}
}//看看 实现了你要的功能
{
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();
}
}//看看 实现了你要的功能
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;
}
}
}
{
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();
}
{
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();
}
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());
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());
}