简体   繁体   English

Gridview中的索引超出范围错误

[英]Index was out of range error in Gridview

I add all row CheckBox in my Gridview helping with this article . 我在Gridview添加了所有行CheckBox来帮助完成这篇article

Here is my Calculate Button 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 that, getting strange error like this; 但是,当我这样做时,得到这样的奇怪错误;

在此输入图像描述

How can i solve this problem? 我怎么解决这个问题?

Best Regards, Soner 最好的问候,Soner

Make sure you have DataKeys defined in your GridView definition 确保在GridView定义中定义了DataKeys

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

   </asp:gridview>

Also try adding a check for DataRow like this 还可以尝试像这样添加DataRow检查

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

It could be related to the way youre populating your gridview, or the way youre setting your GridView to store the DataKeys, could you post both codes, where you set up your GridView component on your .aspx page (the html code) and where you populate it? 它可能与您填充gridview的方式有关,或者您设置GridView以存储DataKeys的方式,您是否可以发布两个代码,在.aspx页面上设置GridView组件(html代码)以及您在哪里填充它?

EDIT: 编辑:

I have tried your example, and the only diferrence is that I aquire a datasource from a List intead of a DataSource control, is running perfectly here, and I Ctrl+c/Ctrl+v your code, so take a look; 我试过你的例子,唯一不同的是我从一个DataSource控件的List intead中获取一个数据源,在这里运行得很好,我按Ctrl + c / Ctrl + v你的代码,所以看一看;

public class MyClass { public int productId { get; public class MyClass {public int productId {get; set; 组; } public string MUS_K_ISIM { get; 公共字符串MUS_K_ISIM {get; set; 组; } } }}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM