protected void GV_Search_RowCommand(object sender, GridViewCommandEventArgs e)
{
DataTable dtShopCart = (DataTable)Session["dtShopCart"];
int index = Convert.ToInt32(e.CommandArgument);
int id=Convert.ToInt32(GV_Search.DataKeys[index].Value);
DataRow dr = dtShopCart.Rows.Find(id);//从Session的购物车中搜索要加入购物车的datakeys值然后判断。
if (dr == null)
{
DataRow newRow = dtShopCart.NewRow();
newRow["N"] = Convert.ToInt32(GV_Search.Rows[index].Cells[0].Text);
newRow["Nname"] = Convert.ToInt32(GV_Search.Rows[index].Cells[1].Text);
newRow["Ntype"] = Convert.ToInt32(GV_Search.Rows[index].Cells[2].Text);
newRow["Nsentime"] = Convert.ToInt32(GV_Search.Rows[index].Cells[3].Text);
newRow["Nppri"] = Convert.ToInt32(GV_Search.Rows[index].Cells[4].Text);
newRow["Nnum"] = 1; dtShopCart.Rows.Add(newRow);
}
else
{
dr["Nnum"] = Convert.ToInt32(dr["Nnum"]) + 1;
}
Server.Transfer("MyCart.aspx");
}其中 int id=Convert.ToInt32(GV_Search.DataKeys[index].Value); 调试提示索引超出范围。必须为非负值并小于集合大小。参数名: index。
这个时候index的值为0;
如何解决?
{
DataTable dtShopCart = (DataTable)Session["dtShopCart"];
int index = Convert.ToInt32(e.CommandArgument);
int id=Convert.ToInt32(GV_Search.DataKeys[index].Value);
DataRow dr = dtShopCart.Rows.Find(id);//从Session的购物车中搜索要加入购物车的datakeys值然后判断。
if (dr == null)
{
DataRow newRow = dtShopCart.NewRow();
newRow["N"] = Convert.ToInt32(GV_Search.Rows[index].Cells[0].Text);
newRow["Nname"] = Convert.ToInt32(GV_Search.Rows[index].Cells[1].Text);
newRow["Ntype"] = Convert.ToInt32(GV_Search.Rows[index].Cells[2].Text);
newRow["Nsentime"] = Convert.ToInt32(GV_Search.Rows[index].Cells[3].Text);
newRow["Nppri"] = Convert.ToInt32(GV_Search.Rows[index].Cells[4].Text);
newRow["Nnum"] = 1; dtShopCart.Rows.Add(newRow);
}
else
{
dr["Nnum"] = Convert.ToInt32(dr["Nnum"]) + 1;
}
Server.Transfer("MyCart.aspx");
}其中 int id=Convert.ToInt32(GV_Search.DataKeys[index].Value); 调试提示索引超出范围。必须为非负值并小于集合大小。参数名: index。
这个时候index的值为0;
如何解决?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货