GridView - 索引超出范围

时间:2013-06-07 16:53:20

标签: c# asp.net

我得到的错误如下:

  

指数超出范围。必须是非负数且小于   集合。参数名称:index

我的代码是,

int index = 0;
        GridViewRow gvrow;
        GridViewRow previousRow;
        if (e.CommandName == "Up")
        {
            index = Convert.ToInt32(e.CommandArgument);
            gvrow = GridView2.Rows[index];
            previousRow = GridView2.Rows[index - 1];
            int mobilePriority = Convert.ToInt32(GridView2.DataKeys[gvrow.RowIndex].Value.ToString());
            int mobileId = Convert.ToInt32(gvrow.Cells[0].Text);
            int previousId = Convert.ToInt32(previousRow.Cells[0].Text);
            con.Open();
            cmd = new SqlCommand("update AppointmentMaster set Priority='" + (mobilePriority - 1) + "' where AppointmentId='" + mobileId + "'; update AppointmentMaster set Priority='" + (mobilePriority) + "' where AppointmentId='" + previousId + "'", con);
            cmd.ExecuteNonQuery();
            con.Close();
        }    

1 个答案:

答案 0 :(得分:1)

previousRow = GridView2.Rows[index - 1];

(index - 1)将为-1,因为index == 0。