gridview下拉框

 protected void GVrentDetails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        ListItem NewItem;
        string sql;
        DataTable dt;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList drop01 = (DropDownList)e.Row.FindControl("ddlRentType");
            sql = "select * from dict where fieldMark='rentType'";
            dt = commonCls.GetDataTable(sql);
            drop01.DataSource = dt;
            drop01.DataTextField = "fieldValue";
            drop01.DataValueField = "fieldValue";
            drop01.DataBind();
            NewItem = new ListItem();
            NewItem.Value = "qxz";
            NewItem.Text = "请选择";
            drop01.Items.Insert(0, NewItem);
            drop01.SelectedValue = DataBinder.Eval(e.Row.DataItem, "rentType").ToString();

            DropDownList drop02 = (DropDownList)e.Row.FindControl("ddlInExpType");
            sql = "select * from dict where fieldMark='InExpType'";
            dt = commonCls.GetDataTable(sql);
            drop02.DataSource = dt;
            drop02.DataTextField = "fieldValue";
            drop02.DataValueField = "fieldValue";
            drop02.DataBind();
            NewItem = new ListItem();
            NewItem.Value = "qxz";
            NewItem.Text = "请选择";
            drop02.Items.Insert(0, NewItem);
            drop02.SelectedValue = DataBinder.Eval(e.Row.DataItem, "InExpType").ToString();
        }
    }
View Code

在RowDataBound事件中绑定下拉框,设置值drop01.SelectedValue = DataBinder.Eval(e.Row.DataItem, "rentType").ToString();

原文地址:https://www.cnblogs.com/zhaolijing910/p/3106387.html