我在Visual Studio 2010中使用Windows Forms for C#。
我正在为商店编写UI。
我有一个附加到折扣数据库表的组合框,显示该表中的DiscountDescription。
我希望能够根据用户在Combobox中的选择来检索DiscountPercent,以便我可以计算出准确的价格。
数据库表:折扣(列的快速示例)
DiscountID:1,2,3,4,5,6(主键)
DiscountPercent:0,10,10,15,20,10
DiscountDescription:NONE,EMPLOYEE,IRVING,MILITARY,MANAGER,SENIOR
如何使用组合框选择从此表中获取DiscountPercent?
每当我更改组合框中的选择时,它是否也可以根据选定的值运行查询?
到目前为止我所拥有的一个例子。
/private void discountDescriptionComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
//Geting the DiscountId
int id = int.Parse(cboDiscount.SelectedValue.ToString());
//dt = your data. You better know about your datasource. I assume there can be a datatable
DataRow[] dr = nISSANDataset["Discount"].select('DiscountId = ' + id);
txtDiscount.Text = dr[0]["DiscountPercent"].ToString();
}
//This fills the combobox
private void PartsInvoice_Load(object sender, EventArgs e)
{
try
{
this.discountTableAdapter.FillDiscount(this.nISSANDataset.Discount);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
答案 0 :(得分:0)
DiscountID
加载组合框。好。细。DiscountID
。DiscountPrice
。DiscountPrice
。> private void comboBox1_SelectedIndexChanged(object sender, EventArgs > e) > { > //Geting the DiscountId > int id = int.Parse(cboDiscount.SelectedValue.ToString()); > //dt = your data. You better know about your datasource. I assume there can be a datatable > DataRow[] dr = dt.select('DiscountId = ' + id); > textbox1.Text = dr[0]["Discount"].ToString(); > }
编辑:对于数据集
> private void comboBox1_SelectedIndexChanged(object sender, EventArgs > e) > { > //Geting the DiscountId > int id = int.Parse(cboDiscount.SelectedValue.ToString()); > //dt = your data. You better know about your datasource. I assume there can be a datatable > DataRow[] dr = this.nISSANDataset.Tables["Discount"].select("DiscountId = " + id); > textbox1.Text = dr[0]["DiscountPercent"].ToString(); > }