日期:2014-05-17  浏览次数:20528 次

Gridview中选中行的值传递到下一个页面
前台部分代码
<asp:CommandField ShowEditButton="true" ShowDeleteButton="true" ShowSelectButton="true"
  HeaderText="操作" EditText="修改" UpdateText="保存" CancelText="取消" SelectText="详细"  
  DeleteText="删除" ControlStyle-CssClass="LinkButton" />

点击详细,讲这行第一列ID的值传给下一个页面

protected void gvPriceRegion_RowCommand(object sender, GridViewCommandEventArgs e)
  {
  if(e.CommandName.Equals("Select"))
  {
  string id = string.Format("FormulaManage.aspx?ID={0}", this.gvPriceRegion.DataKeys[this.gvPriceRegion.SelectedIndex].Value.ToString());
  Response.Redirect(id);
  }
  }
这么写说报错:Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

this.gvPriceRegion.SelectedIndex的值为-1,而且无论我点任何一行的详细按钮,都是-1;如果this.gvPriceRegion.SelectedIndex+1,传的都是第一行的ID

怎么解决

------解决方案--------------------
void gridview1_SelectedIndexChanged(Object sender, EventArgs e) 

GridViewRow row = gridview1.SelectedRow; 
//row.Cells[2].Text; 
//跳转