gpt4 book ai didi

c# - 组合框的值应显示在文本框中

转载 作者:太空宇宙 更新时间:2023-11-03 12:46:35 24 4
gpt4 key购买 nike

组合框的值应该显示在文本框中。有一个表,其中属性“名称”显示在组合框中。在文本框中选择的值的基础上应该导出该值的属性“价格”。数据库通过模型 ADO.NET 连接。我认为 CHANNEL type "ConnectionString = @"Data Source = .... "这一行是没有必要的,因为我已经连接了数据库,一切正常,一切都被保存和更改。唯一剩下的就是这个结论我文本框中的所需值。我是 C# 的新手,我针对我的问题复习了很多类(class)。他们总是使用我不需要的连接字符串。我使用谷歌翻译器从俄语翻译成英语,所以我很抱歉你被误解了。

   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.

最佳答案

我在你的代码中添加了一个事件 SelectedIndexChangedComboBox 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com