简体   繁体   English

单击表单中的删除按钮后如何在gridview中删除行

[英]How to delete a row in gridview after clicking the delete button in a form

I have an aspx page that has a gridview with 3 fields and one button "Update". 我有一个aspx页面,该页面具有3个字段和一个“更新”按钮的gridview。 When I click on the Update button I'll be redirected to another aspx page that has a form with more information about the entry in the grid view that was selected by clicking the button "Update". 当我单击“更新”按钮时,我将被重定向到另一个aspx页面,该页面具有一个表单,其中包含有关通过单击“更新”按钮选择的网格视图中条目的更多信息。 The form contains more fields and a button "Delete". 该表单包含更多字段和一个按钮“删除”。 When I click the button "Delete" I need to close the opened form and go back to the gridview and delete that entry. 当我单击按钮“删除”时,我需要关闭打开的表单并返回到gridview并删除该条目。 I'm using TemplateField to my gridview. 我在我的gridview中使用TemplateField。

<asp:GridView ID="GridView1" runat="server">
   <Columns>
     <asp:TemplateField ShowHeader="False" HeaderText=" ">
          <ItemTemplate>
              <asp:Button ID="Btn_Update" Text="Update" runat="server" ButtonType="Button" CommandName="update" />
          </ItemTemplate>
      </asp:TemplateField>
        <asp:BoundField DataField="ID" HeaderText="ID" />
        <asp:BoundField DataField="FirstName" HeaderText="First Name" />
        <asp:BoundField DataField="LastName" HeaderText="Last Name"  />
    </Columns>
</asp:GridView>

This is the code after I click the button "Delete" in the form to close it and go back to the gridview: 这是我单击表单中的“删除”按钮以将其关闭并返回到gridview之后的代码:

 protected void btn_Delete_Click(object sender, EventArgs e)
{

    #region Redirect to Page
    Page.ClientScript.RegisterStartupScript(this.GetType(), "RefreshParent", "<script language='javascript'>RefreshParent()</script>");
    Response.Write("<script>window.close();</" + "script>");
    #endregion

    ClearData();
}

How can I delete the row from the gridview after clicking the button "Delete" in the form? 单击表单中的“删除”按钮后,如何从gridview中删除行? Thank you all 谢谢你们

Here's some sample code so you'd get the idea. 这是一些示例代码,因此您可以理解。 It's WPF + C#, not web, but you should get the drift. 它是WPF + C#,而不是Web,但您应该有所作为。 The idea is the same. 想法是一样的。

Your main program: 您的主要程序:

public delegate void DeleteRow(bool doDelete);

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    int selectedRow = 0;
    public DeleteRow deleteRowDelegate;

    public void ReportDelete(bool delete)
    {
        // Delete the row here.
    }

    public MainWindow()
    {
        InitializeComponent();
        deleteRowDelegate += new DeleteRow(ReportDelete);
    }

    private void btnOK_Click(object sender, RoutedEventArgs e)
    {            
        // Here, get the row number to selectedRow.

        SecondaryWin win = new SecondaryWin(deleteRowDelegate);
        win.ShowDialog();

        // At this point, if DELETE was clicked in your secondary window, code would have executed ReportDelete() method.
    }
}

And this would be your secondary window: 这将是您的辅助窗口:

public partial class SecondaryWin : Window
{
    DeleteRow callbackDel;

    public SecondaryWin(DeleteRow callback)
    {
        InitializeComponent();
        callbackDel = callback;
    }

    private void btnDel_Click(object sender, RoutedEventArgs e)
    {
        callbackDel.Invoke(true);
        // Close the window
    }
}

So in your main you register the ReportDelete() method to the DeleteRow delegate, and then pass it into your secondary window. 因此,在您的主窗口中,将ReportDelete()方法注册到DeleteRow委托中,然后将其传递到辅助窗口中。 I've passed it in the constructor, but you could use a different method if you so wishes. 我已经在构造函数中传递了它,但是您可以根据需要使用其他方法。

Then in my secondary window you could call that delegate when you click the DELETE button, and exit that window. 然后,在我的辅助窗口中,当您单击DELETE按钮并退出该窗口时,可以调用该委托。

Back in Main(), whenever the DELETE is clicked in your secondary window it will execute the code inside your ReportDelete() method where you could delete that particular row. 回到Main()中,每当在辅助窗口中单击DELETE时,它将在ReportDelete()方法中执行代码,您可以在其中删除该特定行。 Use selectedRow for this purpose. 为此,请使用selectedRow。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM