下拉的数据源



当我试图给数据源下拉抛出异常时,我不知道哪里错了,控件找不到文本和值字段,当我不给文本和值字段的值被绑定为'变量'时,可以有人为此抛出光。

 public void load()
{
    try
    {
        List<Variable> Div = new List<Variable>();
        List<Variable> Dep = new List<Variable>();
        using (DataContext db = new DataContext())
        {
            var cm = (from c in db.COMPANY_MASTERs
                      select c).FirstOrDefault();
            if (Convert.ToBoolean(cm.PLANT))
            {
                 Div = (from vp in db.PLANT
                           select new Variable()
                           {
                               ID = vp.PLANT_ID,
                               NAME = vp.PLANT_NAME
                           }).ToList();
            }
            if (Convert.ToBoolean(cm.ANIMAL))
            {
                Dep = (from vp in db.ANIMAL
                           select new Variable()
                           {
                               ID = vp.ANIMAL_ID,
                               NAME = vp.ANIMAL_NAME
                           }).ToList();
            }
            //this is what I am trying to do but cant,since the variables are nested                inside if.
              Div.AddRange(Dep);
            ddl1.DataTextField = "NAME";
            ddl1.DataValueField = "ID";
            ddl1.DataSource = Div.ToList();
            ddl1.DataBind();
        }
    }
    catch (Exception ex)
    {
    }
}
  public class Variable
 {
   public string NAME;
   public string ID;
 }

use property

public string NAME {get; set;}

Variable中的NAMEID转换为属性。DataTextFieldDataValueField查找属性而不是字段,尽管它们的名称。

public class Variable
{
    public string NAME { get; set; }
    public string ID { get; set; }
}

相关内容

  • 没有找到相关文章

最新更新