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

标签 c# entity-framework combobox textbox

组合框的值应该显示在文本框中。 有一个表,其中属性“名称”显示在组合框中。在文本框中选择的值的基础上应该导出该值的属性“价格”。数据库通过模型 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/

相关文章:

c# - 尽管有更新冲突,但 EF 没有抛出 DbUpdateConcurrencyException

entity-framework - 在 Linq to Entities 中添加日期

java - 如何 .trim() Vaadin 组合框的显示字段?

c# - 检查 ComboBox 是否包含项目

带有复选框的 JavaFX ComboBox

c# - 带孙子的 .NET DDD 域模式

c# - 通过 web api 调用启动 web 驱动程序 chrome 浏览器

C# ContextMenu 删除默认选择

c# - 从 Excel 文件中读取 — 特定工作表

.net - 将 F# 用于 BLL 实现是否有意义?