Hi smile,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
CssClass="table table-striped table-bordered table-hover" OnRowDeleting="GridView1_RowDeleting"
AutoGenerateColumns="false" Width="100%">
<Columns>
<asp:TemplateField HeaderText="Product Code" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblCode" runat="server" Text='<%# Eval("ItemCode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ItemName" HeaderText="Product Name" ItemStyle-Width="200px" />
<asp:BoundField DataField="Rate" HeaderText="Price" ItemStyle-CssClass="price" ItemStyle-Width="100px" />
<asp:TemplateField HeaderText="Quantity" ItemStyle-Width="200px">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox>
<asp:HiddenField ID="hdnfldQty" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total" ItemStyle-Width="200px">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" Text="0"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[5] { new DataColumn("ItemCode", typeof(int)),
new DataColumn("ItemName", typeof(string)),
new DataColumn("Rate", typeof(string)),
new DataColumn("Quantity", typeof(string)),
new DataColumn("Total",typeof(string)) });
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["Data"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex);
DataTable dt = (DataTable)ViewState["Data"];
dt.Rows[index].Delete();
ViewState["Data"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
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(4) {New DataColumn("ItemCode", GetType(Integer)), New DataColumn("ItemName", GetType(String)), New DataColumn("Rate", GetType(String)), New DataColumn("Quantity", GetType(String)), New DataColumn("Total", GetType(String))})
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("Data") = dt
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
Dim index As Integer = Convert.ToInt32(e.RowIndex)
Dim dt As DataTable = CType(ViewState("Data"), DataTable)
dt.Rows(index).Delete()
ViewState("Data") = dt
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Screenshot