下拉数据源 [英] DropDown datasource
本文介绍了下拉数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我试着给数据源下拉列表中它会抛出异常,我DONO哪里说错了,控制无法找到的文本和值字段,当我不给文字和值个行业的价值被绑定为变量,会有些1掷光这一点。
公共无效负载()
{
尝试
{
清单<变量> DIV =新的List<变量>();
清单<变量>德普=新的List<变量>();
使用(DataContext的DB =新的DataContext())
{
VAR厘米=(从db.COMPANY_MASTERsÇ
选择C).FirstOrDefault(); 如果(Convert.ToBoolean(cm.PLANT))
{
DIV =(从db.PLANT副总裁
选择新的变量()
{
ID = vp.PLANT_ID,
NAME = vp.PLANT_NAME
})了ToList()。
}
如果(Convert.ToBoolean(cm.ANIMAL))
{
DEP =(VP来自于db.ANIMAL
选择新的变量()
{
ID = vp.ANIMAL_ID,
NAME = vp.ANIMAL_NAME
})了ToList()。
}
//这是我所试图做的,但不能,因为变量嵌套如果。 Div.AddRange(DEP); ddl1.DataTextField =NAME;
ddl1.DataValueField =ID;
ddl1.DataSource = Div.ToList();
ddl1.DataBind(); }
}
赶上(异常前)
{
}
}
公共类变量
{
公共字符串名称;
公共字符串ID;
}
解决方案
使用属性
公共字符串名称{;组;}
When I try to give datasource to dropdown it throws exception, I dono where am wrong,the control cant find the text and value field,when i dont give text and value fileds the value is binded as 'variables',can some one throw light for this.
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;}
这篇关于下拉数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文