Hi akhter,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="gvtrans" runat="server" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF"
BorderStyle="None" BorderWidth="1px" CellPadding="3" CssClass="active right" GridLines="Horizontal" itemstyle-wrap="True"
ShowFooter="True" ShowHeaderWhenEmpty="True" Style="margin-top: 0px" Width="667px" Height="86px" DataKeyNames="Id"
OnRowDeleting="gvtrans_RowDeleting">
<AlternatingRowStyle BackColor="#F7F7F7" CssClass="altrowstyle" />
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" CssClass="headerstyle" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" CssClass="rowstyle" ForeColor="#4A3C8C" />
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
</Columns>
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F4F4FD" />
<SortedAscendingHeaderStyle BackColor="#5A4C9D" />
<SortedDescendingCellStyle BackColor="#D8D8F0" />
<SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3]
{
new DataColumn("Id"),
new DataColumn("Name"),
new DataColumn("Country")
});
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
ViewState["CurrentTable"] = dt;
this.BindGrid();
}
}
protected void BindGrid()
{
gvtrans.DataSource = ViewState["CurrentTable"] as DataTable;
gvtrans.DataBind();
}
protected void gvtrans_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string index = gvtrans.DataKeys[e.RowIndex].Values[0].ToString();
DataTable dt = ViewState["CurrentTable"] as DataTable;
dt.Rows.Remove(dt.Rows[e.RowIndex]);
ViewState["CurrentTable"] = dt;
this.BindGrid();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("Id"),
New DataColumn("Name"),
New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
ViewState("CurrentTable") = dt
Me.BindGrid()
End If
End Sub
Protected Sub BindGrid()
gvtrans.DataSource = TryCast(ViewState("CurrentTable"), DataTable)
gvtrans.DataBind()
End Sub
Protected Sub gvtrans_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
Dim index As String = gvtrans.DataKeys(e.RowIndex).Values(0).ToString()
Dim dt As DataTable = TryCast(ViewState("CurrentTable"), DataTable)
dt.Rows.Remove(dt.Rows(e.RowIndex))
ViewState("CurrentTable") = dt
Me.BindGrid()
End Sub