开发者

Gridview remove items

I have I gridview in which the data source is a List<T>. When I try to remove an item from the gridview in my buttonRemove_Click() function another function which handles the RowDeleting event is invoked where I remove the item from the List<T> as well. The problem is that if I select to remove multiple items from the gridview the index of the gridview and that of my List<T> un-syncs. For example I have 10 items in my gridview and in my List and I try to remove the last two items. Here is how I do it in my buttonRemove_Click function

foreach (GridViewRow row in gridViewItems.Rows)
{
    CheckBox cb = (CheckBox)row.FindControl("checkBox");

    if (cb != null && 开发者_如何学Gocb.Checked)
    {
        gridViewItems.DeleteRow(row.DataItemIndex);
    }
}

Then in the RowDeleting function, I'll first receive the event for the index 8, I removed it. Now when it comes to deleting the last item (index 9), then it'll throw exception because the index is out of range. How do I solve this problem?

I think the problem will be solved if I try removing the rows in reverse order i.e. starting from the highest index. Can anyone tell how can this be done?


GVGLCode1.DataSource = dt;
GVGLCode1.DataBind();

int iCount = GVGLCode1.Rows.Count;
for (int i = 0; i <= iCount; i++)
{
    CheckBox cb = (CheckBox)GVGLCode1.rows[i].FindControl("checkBox");
    if (cb != null && cb.Checked)
    { 
       GVGLCode1.DeleteRow(i);
    }
}

Please try with this. May be it can help u.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜