日期:2014-05-20  浏览次数:20753 次

LINQ怎样绑定GridView中的DropDownList?
新建了一个方法,
C# code

    public void DDL_PersonName()
    {
        JSJXXKDBDataContext lqDB = new JSJXXKDBDataContext(ConfigurationManager.ConnectionStrings["JSJXXZX_DBConnectionString"].ConnectionString.ToString());
        var result = from per in lqDB.a_employee_mi
                     where per.deleted_flag == '0'
                     select new
                     {
                         name = per.name
                     };
DropDownList1  //在这里,调不出DropDownList1 ,
        
    }

前台GridView中的DropDownList,怎样绑定呢?
C# code

                <asp:TemplateField HeaderText="选吧">
                <ItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" DataSource='<%#DDL_PersonName()%>'> // 不好用
                     
                    </asp:DropDownList>
                </ItemTemplate>
                </asp:TemplateField>


不知道如何用 LINQ 中的数据,绑定 GridView 中的 DropDownList。
非常感谢!


------解决方案--------------------
在 GridView 的初始化事件中绑定,直接点不出来 可以用在GridView 中找控件,数据源的话,直接绑定,跟linq 没多大关系。
------解决方案--------------------
DropDownList 要这样来获取:
DropDownList ddl =GridView1.Rows[0].FindControl("DropDownList1") as DropDownList;

不过你应该按照1楼所说的
放在GV的初始化事件中去绑定数据:
gridview1_databinding 事件
------解决方案--------------------
在上两楼的基础上
C# code

var hospitals = from h in db.B_Hospital
                                where h.IsDel == 1
                                orderby h.HospitalName
                                select h;
 ddlHospital.DataSource = hospitals;
                ddlHospital.DataTextField = "HospitalName";
                ddlHospital.DataValueField = "ID";
                ddlHospital.DataBind();