DropDownList控件绑定数据库

来源:互联网 发布:网络刺客ii使用教程 编辑:程序博客网 时间:2024/04/30 05:52
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
   if (e.Row.RowState.ToString().Contains("Edit"))  //如果控件在模板列的编辑状态下就加这个条件
           {
            DropDownList dd = e.Row.FindControl("DropDownList1") as DropDownList;
          
            DataClassesDataContext lqDB = new DataClassesDataContext(ConfigurationManager.ConnectionStrings["qiyezhanConnectionString"].ConnectionString.ToString());
            var result = from r in lqDB.Table1
                         where r.id > 0
                         select r;

            dd.DataSource = result;
            dd.DataValueField = "FieldValue";   //绑定的字段名
   dd.DataTextField = "FieldValue";
            dd.DataBind();
            dd.Items.Insert(0, new ListItem("—请选择—", "0"));  //另外添加列表项
   dd.SelectedValue = ((Label)(e.Row.Cells[9].Controls[1])).Text.ToString();  //指定默认列表项,或者直接赋值为"0"表示"请选择"项
                                                                                       
   }
        }
    }
原创粉丝点击