如何在编辑项模板下绑定下拉列表编辑模式在网格视图中出现

时间:2014-05-12 09:40:00

标签: c# asp.net-4.0

我只想在行编辑模式下的网格视图中动态绑定两个下拉列表。在这里,我声明了一个代码块,它动态地获取该行状态并绑定这两个下拉列表。

这是代码:

  protected void GV_ViewCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
          ...............
    if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
            {
                using (DataClassesDataContext db = new DataClassesDataContext())
                {
                    DropDownList dl = (DropDownList)e.Row.FindControl("DDL_Types1");
                    dl.DataSource = db.PartyTypes.Select(t => t).ToList();
                    dl.DataBind();
                    dl.SelectedValue = DataBinder.Eval(e.Row.DataItem, "type_id").ToString();
                    DropDownList dl1 = (DropDownList)e.Row.FindControl("DDL_CountryNames1");
                    dl1.DataSource = db.Countries.Select(c => c).ToList();
                    if (!string.IsNullOrEmpty(DataBinder.Eval(e.Row.DataItem, "country_id").ToString()))
                    {
                        dl1.SelectedValue = DataBinder.Eval(e.Row.DataItem, "country_id").ToString();
                        DropDownList dl2 = (DropDownList)e.Row.FindControl("DDL_StateNames1");
                        dl2.DataSource = db.States.Where(s => s.country_id.Equals(int.Parse(DataBinder.Eval(e.Row.DataItem, "country_id").ToString()))).Select(s => s).ToList();
                        dl2.DataBind();
                    }
                    DataRowView rowView1 = (DataRowView)e.Row.DataItem;
                    if (rowView1["UserOFC"] != null)
                    {
                        (e.Row.FindControl("chk_UserOFC1") as CheckBox).Checked = Convert.ToBoolean(e.Row.DataItem.Equals("UserOFC").ToString());
                    }
                    if (rowView1["UserVAT"] != null)
                    {
                        (e.Row.FindControl("chk_UserVAT1") as CheckBox).Checked = Convert.ToBoolean(e.Row.DataItem.Equals("UserVAT").ToString());
                    }
                    if (rowView1["UserINV"] != null)
                    {
                        (e.Row.FindControl("chk_UserINV1") as CheckBox).Checked = Convert.ToBoolean(e.Row.DataItem.Equals("UserINV").ToString());
                    }
                    if (rowView1["UserNone"] != null)
                    {
                        (e.Row.FindControl("chk_UserNone1") as CheckBox).Checked = Convert.ToBoolean(e.Row.DataItem.Equals("UserNone").ToString());
                    }
                }
            }
..................
}

这是在行编辑模式下绑定下拉列表的正确方法。

请帮帮我....

1 个答案:

答案 0 :(得分:0)

很好。但还有其他一些方法。