下拉绑定方法不起作用

时间:2012-02-21 10:31:35

标签: c# asp.net linq drop-down-menu

我有一个方法:

public IEnumerable<Company> Select()
{
   DataClassesCompanyDataContext dc = new DataClassesCompanyDataContext();
   System.Data.Linq.Table<Company> company = dc.GetTable<Company>();
   Company com = new Company();
   return dc.Companies.OrderBy(e => e.company_id);
}

通过这个我想绑定下拉列表。

我正在使用这种方法:

private void fillCompanyNameDropdown()
{
    PioneerDataAccess.CompanyClass comp = new PioneerDataAccess.CompanyClass();
    var objcomp = comp.Select().ToList();
    ddlCompanyOff.DataSource = objcomp[0].company_name;

    ddlCompanyOff.DataBind();
}

但问题是,如果公司名称是pulil,那么它会显示

p
u
l
i
l

那我该怎么办?

1 个答案:

答案 0 :(得分:4)

您的代码......这可能会解决您的问题

var objcomp = comp.Select().ToList();             
ddlCompanyOff.DataSource = objcomp ;
ddlCompanyOff.DataTextField = "company_name";              
ddlCompanyOff.DataValueField = "company_name";              
ddlCompanyOff.DataBind();