English 中文(简体)
视图网格删除行
原标题:gridview delete rows

我正在试图从网格视图中删除行 - 但当我这样做时,它会从其他行的其他控件中删除当前值 。

删除随机行后, 我也会收到以下错误 。

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

protected void gvShoppingCart_RowCommand(object sender, GridViewCommandEventArgs e)
{
    //This is the method that responds to the Remove button s click event
    #region remove item

    if (e.CommandName == "Remove")
    {
        int rowid = Convert.ToInt32(e.CommandArgument);
        //Response.Write(rowid);
        // Response.End();
        //Gridview1.DeleteRow(rowid);
        //Response.Write(Convert.ToInt32(e.CommandArgument));
        // Response.End();
        int rowIndex = rowid;
        if (ViewState["CurrentTable"] != null)
        { 
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];

            DataRow drCurrentRow = null;

            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 0; i < dtCurrentTable.Rows.Count; i++)
                {
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex - 1].Cells[1].FindControl("TextBox1");

                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex - 1].Cells[2].FindControl("TextBox2");

                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex - 1].Cells[3].FindControl("TextBox3");

                    TextBox box4 = (TextBox)Gridview1.Rows[rowIndex - 1].Cells[4].FindControl("TextBox4");
                    TextBox box5 = (TextBox)Gridview1.Rows[rowIndex - 1].Cells[5].FindControl("TextBox5");
                    box1.Text = dtCurrentTable.Rows[i]["Column1"].ToString();

                    box2.Text = dtCurrentTable.Rows[i]["Column2"].ToString();

                    box3.Text = dtCurrentTable.Rows[i]["Column3"].ToString();

                    box4.Text = dtCurrentTable.Rows[i]["Column4"].ToString();
                    box5.Text = dtCurrentTable.Rows[i]["Column5"].ToString();
                }

                dtCurrentTable.Rows.RemoveAt(rowid - 1);

                /*dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;

                dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;

                dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
                dtCurrentTable.Rows[i - 1]["Column4"] = box4.Text;
                dtCurrentTable.Rows[i - 1]["Column5"] = box5.Text;*/
            }

            ViewState["CurrentTable"] = dtCurrentTable;

            Gridview1.DataSource = dtCurrentTable;

            Gridview1.DataBind();   
        }
        else
        {
            Response.Write("ViewState is null");
        }
    }
}

谢谢你的帮忙

问题回答

将条件更改为...

if (dtCurrentTable.Rows.Count > 0 && dtCurrentTable.Rows.Count > rowIndex)

NOTE: are you sure e.CommandArgument is giving index of that particular row ?





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签