简体   繁体   中英

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". 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". 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. I'm using TemplateField to my gridview.

<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:

 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? 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. 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. 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.

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. Use selectedRow for this purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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