组合框的值应显示在文本框中。
有一个表格,其中的属性“名称”显示在组合框中。根据在文本框中选择的值,应派生该值的“价格”属性。数据库通过ADO.NET模型连接。
我认为这行CHANNEL类型不是“ ConnectionString = @” Data Source =...。”,因为我已经连接了数据库,所以一切正常,一切都保存并更改了。剩下的唯一结论就是我的结论我是C#的新手,我复习了很多有关此问题的课程,他们总是使用此连接字符串,而我不需要。
我使用的是Google俄语翻译成英语的翻译器,所以对您造成的误解我深感抱歉。
namespace test6
{
public partial class Form5 : Form
{
centrEntities db;
public Form5()
{
InitializeComponent();
FillCombobox();
}
private void Form5_Load(object sender, EventArgs e)
{
db = new centrEntities();
db.Configuration.ProxyCreationEnabled = false;
db.Configuration.LazyLoadingEnabled = false;
orderBindingSource.DataSource = db.order.ToList();
}
private void FillCombobox()
{
using (centrEntities c = new centrEntities())
{
comboService.DataSource = c.service.ToList();
comboService.ValueMember = "serviceID";
comboService.DisplayMember = "name";
}
}
Table - values
how it looks.
最佳答案
我在您的代码中向SelectedIndexChanged
ComboBox
添加了一个事件comboService
,如下所示:
public partial class Form5 : Form
{
centrEntities db;
public Form5()
{
InitializeComponent();
FillCombobox();
comboService.SelectedIndexChanged += new EventHandler(comboService_SelectedIndexChanged);
}
private void Form5_Load(object sender, EventArgs e)
{
db = new centrEntities();
db.Configuration.ProxyCreationEnabled = false;
db.Configuration.LazyLoadingEnabled = false;
orderBindingSource.DataSource = db.order.ToList();
}
private void FillCombobox()
{
using (centrEntities c = new centrEntities())
{
comboService.DataSource = c.service.ToList();
comboService.ValueMember = "serviceID";
comboService.DisplayMember = "name";
}
}
private void comboService_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboService.SelectedValue != null)
{
using (centrEntities c = new centrEntities())
{
var price = (from serv in c.service
where serv.serviceID == Convert.ToInt32(comboService.SelectedValue)
select serv.price).SingleOrDefault();
TextPriceName.Text = price.ToString();
}
}
}
}
关于c# - 组合框的值应显示在文本框中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37089622/