c#-4.0 - 如何在行数据绑定(bind)的编辑模式下检测 GridView

标签 c#-4.0 asp.net-4.0

我只想在行编辑模式下检测 GridView ,并且此时我只想绑定(bind)下拉列表。我找到了很多文章,我编写了这段代码:

 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();
            }
        }
}

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

请帮助我...

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();
    }

最佳答案

不确定您的问题实际上是什么,但您还需要检查页脚中的 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)
    {
        // ...
    }
}

关于c#-4.0 - 如何在行数据绑定(bind)的编辑模式下检测 GridView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23887509/

相关文章:

asp.net - 我需要指纹 virdi 的 SDK 和示例来查看如何读取拇指并验证它?

c++ - 如何将指针函数从 C++ 导出到 C#?

c# - ASP.Net 4.0 - 站点地图构建中需要响应吗?

asp.net - 在 .NET 4 下编译时出现 "An entry with the same key already exists"

linq-to-sql - ASP.NET MVC 2 + LINQ to SQL - CS0012 编译错误

winforms - C# - 通过特殊方式给图片添加水印

c# - 动态创建 Func<> - Lambda 与表达式树

c# - 从 VS2008 升级到 2010 后,Crystal Reports 工具栏不见了

c# - 制作一个 Buttonfield 执行一个 javascript