0

i just want to bind two drop down list dynamically when grid view in row editing mode. here i declares one code block that dynamically fetches that row state and binds up those two drop down list.

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

Is this right method to binds drop down list at row editing mode.

please help me....

Shal
  • 319
  • 4
  • 8
  • 25

1 Answers1

0

It's fine.But there are some other ways.

tarzanbappa
  • 4,930
  • 22
  • 75
  • 117
  • As I mentioned in the above comment. You can bind the data in RowDataBinding Event. And also if you use jquery or java script to bind data,you can use javascript or jquery methods to bind the data. – tarzanbappa May 12 '14 at 10:06