Index was out of tolerance in gridview

I am adding all the lines CheckBoxto mine Gridview, helping with this article.

Here is my code Calculate Button;

protected void Calculate_Click(object sender, EventArgs e)
    {
        bool atLeastOneRowDeleted = false; 

        foreach (GridViewRow row in GridView1.Rows) 
        { 
            CheckBox cb = (CheckBox)row.FindControl("ProductSelector"); 
            if (cb != null && cb.Checked)
            { 
                atLeastOneRowDeleted = true; 
                int productID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value); 
                Response.Write(string.Format( "This would have deleted ProductID {0}<br />", productID));
            }
        }

    }

But when I do this, I get a strange error, for example:

enter image description here

How can I solve this problem?

Regards, Soner

+3
source share
2 answers

Verify that the DataKeys parameters are defined in the GridView definition

  <asp:gridview id="GridView2" 
        datakeynames="productID"
        ...
        ...>
    enter code here

   </asp:gridview>

Also try adding check for DataRow like this

    foreach (GridViewRow row in GridView1.Rows) 
    {
      if(row.RowType == DataControlRowType.DataRow)
      { 
        CheckBox cb = (CheckBox)row.FindControl("ProductSelector"); 
        if (cb != null && cb.Checked)
        { 
            atLeastOneRowDeleted = true; 
            int productID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value); 
            Response.Write(string.Format( "This would have deleted ProductID {0}<br />", productID));
        }
      }
    }
+4
source

This may be due to how you populate the gridview or how you configure the GridView to store DataKeys, can you post both codes where you configured your GridView component on the .aspx page (html code) and where you populate it?

EDIT:

, , List, DataSource, , Ctrl + c/Ctrl + v , ;

MyClass       {           public int productId {get; ; }           public string MUS_K_ISIM {get; ; }       }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<MyClass> ChechkBoxDataSource = new List<MyClass>();
            ChechkBoxDataSource.Add(new MyClass() { productId = 1, MUS_K_ISIM = "Stack" });
            ChechkBoxDataSource.Add(new MyClass() { productId = 2, MUS_K_ISIM = "Overflow" });
            ChechkBoxDataSource.Add(new MyClass() { productId = 3, MUS_K_ISIM = "Example" });

            GridView1.DataSource = ChechkBoxDataSource;
            GridView1.DataBind();
        }
    }

    protected void Unnamed1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox cb = (CheckBox)row.FindControl("ProductSelector");
                if (cb != null && cb.Checked)
                {
                    int productID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
                    Response.Write(string.Format("This would have deleted ProductID {0}<br />", productID));
                }
            }
        }
    }
+2

All Articles