在GridView中按该行上的编辑时,从特定列获取数据

时间:2013-07-09 20:35:44

标签: c# asp.net gridview

如何从网格视图中按下编辑按钮的特定列中获取数据?

以下代码不起作用:

protected void viewStoryTime_OnRowEditing(object sender, GridViewEditEventArgs e)
{
    SqlDataSource10.UpdateParameters["setEditHoursParam"].DefaultValue = viewStoryTime.SelectedRow.Cells[0].Text;
}

这是因为实际上没有选择该行。我怎么能做到这一点?

2 个答案:

答案 0 :(得分:2)

e.NewEditIndex具有当前编辑行的行索引。您可以使用它来访问行并根据需要读取单元格数据。

答案 1 :(得分:1)

参数GridViewEditEventArgs包含gridview中当前编辑行的行索引。

你应该做这样的事情

SqlDataSource10.UpdateParameters["setEditHoursParam"].DefaultValue = MyGridView.Rows[e.NewEditIndex].Cells[0].Text;

另一种方法是实现一个RowCommand事件,其中参数GridViewCommandEventArgs带有命令名,那么你应该这样做:

void MyGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
  // If multiple buttons are used in a GridView control, use the
  // CommandName property to determine which button was clicked.
  if(e.CommandName=="my command name")
  {
     //Do stuff here
  }
}