如何在行数据绑定时检测编辑模式下的网格视图

时间:2014-05-27 10:52:30

标签: c#-4.0 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());
                }
            }
            UpdatePanel10.Update();
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                DropDownList ddl1 = (DropDownList)e.Row.FindControl("DDL_Types");
                ddl1.DataSource = db.PartyTypes.Select(p => p).ToList();
                ddl1.DataBind();
                DropDownList ddl2 = (DropDownList)e.Row.FindControl("DDL_CountryNames");
                ddl2.DataSource = db.Countries.Select(c => c).ToList();
                ddl2.DataBind();
            }
        }
}

如何成功将页脚下拉列表绑定,但在编辑项目模板中没有。

请帮帮我......

protected void GV_ViewCustomers_RowEditing(object sender, GridViewEditEventArgs e)
    {
       GV_ViewCustomers.EditIndex = e.NewEditIndex;
       this.FillGrid((String)Session["StartAlpha"] ?? null, (int)Session["GroupByENTYPE"] , (String)Session["ColumnName"] ?? null, (String)Session["SearchText"] ?? null);
       UpdatePanel10.Update();
       MPE.Show();
    }

2 个答案:

答案 0 :(得分:3)

不确定您的问题究竟是什么,但您还需要在页脚中检查RowState=Edit

所以而不是:

protected void GV_ViewCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
    {
        // ...
    }
    if (e.Row.RowType == DataControlRowType.Footer)
    {
        // ...
    }
}

这样:

protected void GV_ViewCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow
        && e.Row.RowState == DataControlRowState.Edit)
    {
        // ...
    }
    else if (e.Row.RowType == DataControlRowType.Footer 
        && e.Row.RowState == DataControlRowState.Edit)
    {
        // ...
    }
}

答案 1 :(得分:0)

PATH

{{3}}