GridView中删除行的方法

发布于 2024-08-31 08:33:19 字数 93 浏览 6 评论 0原文

我正在尝试实现删除方法并传递删除操作的参数。我正在使用sqldatasource。由于 ID 在我的 gridview 中没有列,如何获取 ID 的值并将其设置为删除参数?

I am trying to implement the deleting method and pass my parameters for the delete operation. I am using sqldatasource. Since the ID doesnt have a column in my gridview how can I get the value of the ID and set it as my delete parameter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌路黄昏 2024-09-07 08:33:19

ID 不需要 GridView 中的列,只要它位于数据源中即可。这是一个简单的 GridView 教程,解释了该过程...

http://www.aspdotnetcodes.com/GridView_Insert_Edit_Update_Delete.aspx

他们的 RowDeleting 处理程序...

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
  customer.Delete(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
  FillCustomerInGrid();
}

ID doesn't need a column in the GridView, as long as it is in the datasource. Here's a simple GridView tutorial explaining the procedure...

http://www.aspdotnetcodes.com/GridView_Insert_Edit_Update_Delete.aspx

Their RowDeleting handler...

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
  customer.Delete(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
  FillCustomerInGrid();
}
最美的太阳 2024-09-07 08:33:19

您可以设置一个方法来处理 gridview 的 OnRowDeleting 事件,取消那里的删除,并实现您自己的逻辑来执行删除(即基于 id 之外的某些字段进行删除):

<asp:GridView OnRowDeleting="gridview_rowdeleting" />


protected void gridview_rowdeleting(Object sender, GridViewDeleteEventArgs e)
    {
    e.Cancel = true;
        // logic for performing delete here...e.Rows returns the collection of deleted rows so you can access whatever values you need...e.Rows[0].Cells[0] gives the value in the first column for the first deleted row for example
    }

You can set up a method to handle the gridview's OnRowDeleting event, cancel the delete there, and implement your own logic to perform the delete (ie delete based on some field besides id):

<asp:GridView OnRowDeleting="gridview_rowdeleting" />


protected void gridview_rowdeleting(Object sender, GridViewDeleteEventArgs e)
    {
    e.Cancel = true;
        // logic for performing delete here...e.Rows returns the collection of deleted rows so you can access whatever values you need...e.Rows[0].Cells[0] gives the value in the first column for the first deleted row for example
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文