Shal
Shal

Reputation: 329

how to detect grid view in edit mode at row data bound

i just want to detect that grid view in row editing mode and i just want to bind drop down list at this moment. i found many article and i make this code :

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

how ever footer drop down list successfully binds up but in edit item template not.

please help me...

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

Upvotes: 3

Views: 16579

Answers (2)

Salman AL-Shehri
Salman AL-Shehri

Reputation: 1

if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState ==
(DataControlRowState.Edit | DataControlRowState.Alternate))
{
  // write your statement that you want to fire in Edit mode
}

http://www.dotnetfunda.com/codes/show/123/how-to-check-for-edit-mode-of-the-gridview-row-irrespective-of-it-is-n

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460028

Not sure what your issue actually is, but you need to check for RowState=Edit also in the footer.

So instead of:

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)
    {
        // ...
    }
}

this:

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)
    {
        // ...
    }
}

Upvotes: 5

Related Questions