-1

i have editable grid view which have check boxes which binded with data row. here i put code :

 protected void GV_ViewCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
    { 
        if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState != DataControlRowState.Edit)
    {
        using (DataClassesDataContext db = new DataClassesDataContext())
        {
            DropDownList ddl = (DropDownList)e.Row.FindControl("DDL_Types");
            ddl.DataSource = db.PartyTypes.Select(p => p).ToList();
            ddl.DataBind();
            ddl.SelectedValue = DataBinder.Eval(e.Row.DataItem, "type_id").ToString();
            DropDownList ddl1 = (DropDownList)e.Row.FindControl("DDL_CountryNames");
            ddl1.DataSource = db.Countries.Select(c => c).ToList();
            ddl1.DataBind();
            if (!string.IsNullOrEmpty(DataBinder.Eval(e.Row.DataItem, "country_id").ToString()))
            {
                ddl1.SelectedValue = DataBinder.Eval(e.Row.DataItem, "country_id").ToString();
                DropDownList ddl2 = (DropDownList)e.Row.FindControl("DDL_StateNames");
                ddl2.DataSource = db.States.Where(s => s.country_id.Equals(int.Parse(DataBinder.Eval(e.Row.DataItem, "country_id").ToString()))).Select(s => s).ToList();
                ddl2.DataBind();
                ddl2.SelectedValue = DataBinder.Eval(e.Row.DataItem, "state_id").ToString();
            }
        }
        DataRowView rowView1 = (DataRowView)e.Row.DataItem;
        if (rowView1["UserOFC"] != null)
        {
            (e.Row.FindControl("chk_UserOFC") as CheckBox).Checked = Convert.ToBoolean(e.Row.DataItem.Equals("UserOFC").ToString());
        }
        if (rowView1["UserVAT"] != null)
        {
            (e.Row.FindControl("chk_UserVAT") as CheckBox).Checked = Convert.ToBoolean(e.Row.DataItem.Equals("UserVAT").ToString());
        }
        if (rowView1["UserINV"] != null)
        {
            (e.Row.FindControl("chk_UserINV") as CheckBox).Checked = Convert.ToBoolean(e.Row.DataItem.Equals("UserINV").ToString());
        }
        if (rowView1["UserNone"] != null)
        {
            (e.Row.FindControl("chk_UserNone") as CheckBox).Checked = Convert.ToBoolean(e.Row.DataItem.Equals("UserNone").ToString());
        }
    }
    }

this all check boxes values coming from allow null data column so there need to bind them from grid view row data bound.

Khan
  • 344
  • 1
  • 7
  • 21
shalsoft
  • 457
  • 1
  • 5
  • 13
  • The part where you are assigning the .Checked property looks OK, are you getting any exception running this? Or is it just not working? Can you debug and check what gets assigned? It might help to seperate the FindControl call to assign the control to a variable, then assign the Checked property and see what actually is getting assigned by the Convert.ToBoolean calls. – Zack Jun 04 '14 at 13:50
  • no it just not working how ever drop down list getting binds. – shalsoft Jun 04 '14 at 13:52
  • @Zack can u understand what problem is that here. – shalsoft Jun 05 '14 at 09:38

1 Answers1

0

Not an answer, but it should help with debugging to see what the problem is. Seperate the logic where you assign the checked property of the CheckBox controls, and check what is being assigned to the Checked property with the debugger.

CheckBox chk_UserOFC = e.Row.FindControl("chk_UserOFC") as CheckBox;
bool UserOFC = Convert.ToBoolean(e.Row.DataItem.Equals("UserOFC").ToString());
chk_UserOFC.Checked = UserOFC;

Also, why are you using e.Row.DataItem.Equals, which returns a bool, then casting that to a string, then using Convert.ToBoolean to get a bool? I think you can use this just the same:

bool UserOFC = e.Row.DataItem.Equals("UserOFC");
Zack
  • 2,789
  • 33
  • 60