Please refer this article
Add the LinkButton for deleting the records in above article GridView.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowEditing="OnRowEditing">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" Text="Edit" runat="server" CommandName="Edit" />
<asp:LinkButton ID="LinkButton4" Text="Delete" runat="server" OnClick="OnDelete" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton2" Text="Update" runat="server" OnClick="OnUpdate" />
<asp:LinkButton ID="LinkButton3" Text="Cancel" runat="server" OnClick="OnCancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
LinkButton Onclick event for Deleting the Rows.
VB.Net
Protected Sub OnDelete(sender As Object, e As EventArgs)
Dim row As GridViewRow = TryCast(TryCast(sender, LinkButton).NamingContainer, GridViewRow)
Dim dt As DataTable = TryCast(ViewState("dt"), DataTable)
dt.Rows.Remove(dt.Rows(row.RowIndex))
ViewState("dt") = dt
Me.BindGrid()
End Sub
C#
protected void OnDelete(object sender, EventArgs e)
{
GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
DataTable dt = ViewState["dt"] as DataTable;
dt.Rows.Remove(dt.Rows[row.RowIndex]);
ViewState["dt"] = dt;
this.BindGrid();
}
For inserting the GridView Rows please refer this article
You can also use For loop to insert the GridView Rows in Database.
Refer:
- Bulk Insert Data from GridView Rows into Database in ASP.Net