ComboBox的SelectedIndexChanged事件-填充控件值

时间:2018-09-02 19:48:08

标签: c# winforms entity-framework-6

当ComboBox SelectedIndexChanged事件发生时,我需要用其值填充各个控件。我正在将实体框架6用于该方法。我正在使用C#Windows窗体和.net Framework 4.5。我的代码是:

private void comboBoxCustomer_SelectedIndexChanged(object sender, EventArgs e)
{
    using (ReliableTradersEntities entities = new ReliableTradersEntities())
    {
        m_blouse = new BLOUSE_MEASUREMENT();
        m_pardi = new PARDI_MEASUREMENT();
        m_lengha = new LHENGA_MEASUREMENT();

        var res = from c in entities.CUSTOMERs
                        join p in entities.PARDI_MEASUREMENT on c.CUSTOMERID equals p.CUSTOMERID
                        join l in entities.LHENGA_MEASUREMENT on c.CUSTOMERID equals l.CUSTOMERID
                        join b in entities.BLOUSE_MEASUREMENT on c.CUSTOMERID equals b.CUSTOMERID
                        select new 
                        {

                            p.PARDILENGTH,
                            p.PARDILIMIT,
                            p.SHOULDER,
                            p.SHOULDERTOHEAD,
                            p.HEADTOBACK,
                            p.HALFHEADROUND,
                            p.NIQABLENGTH,
                            p.KAS,
                            l.LHENGALENGTH,
                            l.LHENGALIMIT,
                            l.BELTOPTION,
                            l.ISPOCKET,
                            l.ISZIP,
                            l.STYLE,
                            l.STYLESIZE,
                            l.ALINE,
                            b.BLOUSELENGTH,
                            b.CHEST,
                            b.WAIST,
                            b.SLEEVES,
                            b.NECK,
                            b.POINT,
                            b.BLOUSEOPENING
                        };
        textBoxPL.Text = m_pardi.PARDILENGTH;
    }

}

1 个答案:

答案 0 :(得分:1)

您已经创建了一个匿名类型,该匿名类型将由查询提取的所有信息返回到变量 res 中。您尚未填写最初的三个类实例。

您需要使用 res 变量

初始化实例
// The query returns an IEnumerable, you need to extract the first element
var x = res.FirstOrDefault();
if(x != null)
{
    m_pardi = new PARDI_MEASUREMENT();
    m_pardi.PARDILENGTH = x.PARDILENGTH;
    .... 
}

但是您可以直接使用 res 变量

 var x = res.FirstOrDefault();
 if(x != null)
 {
     textBoxPL.Text = x.PARDILENGTH;
     .... and so on....
 }