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

时间:2016-05-07 14:17:22

标签: c# entity-framework combobox textbox

组合框的值应显示在文本框中。 有一个表格,其中属性" name"显示在组合框中。在文本框中选择的值的基础上应该派生属性" price"这个值。数据库通过ADO.NET模型连接。 我认为行CHANNEL类型" ConnectionString = @"数据源= ...."没有必要,因为我已经连接的数据库,一切正常,一切都被保存和更改。唯一剩下的就是这个结论,我希望文本框中的值。我是C#的新手,我回顾了一堆关于我的问题的课程。他们总是使用这个我不需要的连接字符串。 我使用了从俄语到英语的谷歌翻译,所以我很抱歉被误解了。

   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.

1 个答案:

答案 0 :(得分:1)

我已将代码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();
            }
        }
    }
}