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

C# 在GridView中按该行上的“编辑”键时,从特定列获取数据,c#,asp.net,gridview,C#,Asp.net,Gridview,如何从gridview中按下编辑按钮的特定列中获取数据 以下代码不起作用: protected void viewStoryTime_OnRowEditing(object sender, GridViewEditEventArgs e) { SqlDataSource10.UpdateParameters["setEditHoursParam"].DefaultValue = viewStoryTime.SelectedRow.Cells[0].Text; } 这是因为该行实际上未被

如何从gridview中按下编辑按钮的特定列中获取数据

以下代码不起作用:

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

这是因为该行实际上未被选中。如何完成此操作?

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

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

参数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
  }
}

参数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
  }
}

有趣的是,我在寻找这个答案,MVC让这个很容易,你可以把id和链接一起传递有趣的是,我在寻找这个答案,MVC让这个很容易,你可以把id和链接一起传递