0

I want to make a multiple delete checkbox where user can delete multiple data from gridview easily. However I stumble an error:

System.NullReferenceException: Object reference not set to an instance of an object.

Line 242:               if (chk != null)
Line 243:               {
Line 244:                   chk.Checked = arr.Contains(grdadmin.DataKeys[i].Value);
Line 245:                   if (!chk.Checked)
Line 246:                       chkAll.Checked = false;

There it shows where the error occur, can someone help me, what need to be fixed?

private void SetData()
    {
        int currentCount = 0;
        CheckBox chkAll = (CheckBox)grdadmin.HeaderRow.Cells[0].FindControl("chkAll");
        chkAll.Checked = true;
        ArrayList arr = (ArrayList)ViewState["SelectedRecords"];
        for (int i = 0; i < grdadmin.Rows.Count; i++)
        {
            CheckBox chk = (CheckBox)grdadmin.Rows[i].Cells[0].FindControl("chk");
            if (chk != null)
            {
                chk.Checked = arr.Contains(grdadmin.DataKeys[i].Value);
                if (!chk.Checked)
                    chkAll.Checked = false;
                else
                    currentCount++;
            }
        }
        hfCount.Value = (arr.Count - currentCount).ToString(); 
    }

Here is my code, help is really much appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
activ
  • 11
  • 1

1 Answers1

0

Try

 CheckBox chkAll = (CheckBox)grdadmin.HeaderRow.FindControl("chkAll");

(or show the aspx markup)

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939