代码改变世界

gridview中获取主键的值

2009-12-17 00:01  Vincent.Feng  阅读(459)  评论(0编辑  收藏  举报

最近写几个小活,后台用到gridview,把常用到事件里面的取主键值的方式都记录下来,以后方便查询.

 

GridView1_RowCommand中获取主键的值:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int OrderId = Convert.ToInt32(GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value); }

GridView1_PageIndexChanging中获取主键的值

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
int index=GridView1.DataKeys[e.NewPageIndex].Value;
}

GridView1_RowDeleting中获取主键的值
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int index=GridView1.DataKeys[e.RowIndex].Value;
}

GridView1_RowEditing中获取主键的值
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
int index = GridView1.DataKeys[e.NewEditIndex].Value;
}

GridView1_RowUpdating中获取主键的值

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = GridView1.DataKeys[e.RowIndex].Value;
}

GridView1_RowDataBound中获取主键的值
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int index = GridView1.DataKeys[e.Row.RowIndex].Value;
}