1. 首先在初始化Gridview时候定义主键的数组。

 GridViewTeacherStudent.DataKeyNames=new string[] {"courseId","studentId","type","level","unit"};

2. 在进行删除操作,或者对某行进行操作获得列中的值。

 string studentId = GridViewTeacherStudent.DataKeys[e.RowIndex]["studentId"].ToString().Trim();

3.如果只有单个的键值时候。如只有”StudentId“一个主键下面直接获得。

 string studentId = GridViewTeacherStudent.DataKeys[e.RowIndex].Value.ToString().Trim();

 

 

在 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;

  }

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-05
  • 2022-12-23
  • 2021-09-15
  • 2022-02-11
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-19
  • 2022-12-23
  • 2022-12-23
  • 2021-10-15
  • 2022-12-23
  • 2021-09-14
相关资源
相似解决方案