Hi akhter,
You have to specify a select button inside gridview to select a row. then you can delete a row.
HTML
<asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" AllowSorting="True" ShowFooter="True">
<Columns>
<asp:TemplateField HeaderText="Product No">
<ItemTemplate>
<asp:Label ID="lblProductNo" runat="server" Text='<%#Bind("ProductNo")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblDescriptionItem" runat="server" Text='<%#Bind("DescriptionItem")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Order No">
<ItemTemplate>
<asp:Label ID="lblOrderno" runat="server" Text='<%#Bind("OrderNo")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Qty">
<ItemTemplate>
<asp:Label ID="lblQty" runat="server" Text='<%#Bind("Qty")%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server"></asp:Label></FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="True" ButtonType="Button" />
</Columns>
<SelectedRowStyle BackColor="Yellow" />
</asp:GridView><br />
<asp:Button ID="btnDelete" Text="Delete" runat="server" OnClick="OnDeleteRow" />
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[] {
new DataColumn("ProductNo"),
new DataColumn("DescriptionItem"),
new DataColumn("OrderNo"),
new DataColumn("Qty")
});
dt.Rows.Add("1", "ABC", "100", "1");
dt.Rows.Add("2", "Xyz", "200", "2");
dt.Rows.Add("3", "Pqr", "300", "3");
dt.Rows.Add("4", "Lmn", "400", "4");
dt.Rows.Add("5", "Stu", "500", "10");
ViewState["dt"] = dt;
this.BindOrders();
}
}
private void BindOrders()
{
this.gvOrders.DataSource = ViewState["dt"] as DataTable;
this.gvOrders.DataBind();
}
protected void OnDeleteRow(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt = ViewState["dt"] as DataTable;
GridViewRow row = gvOrders.SelectedRow;
int index = row.RowIndex;
if (ViewState["dt"] != null)
{
dt.Rows[index].Delete();
}
this.BindOrders();
}
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() {New DataColumn("ProductNo"), New DataColumn("DescriptionItem"), New DataColumn("OrderNo"), New DataColumn("Qty")})
dt.Rows.Add("1", "ABC", "100", "1")
dt.Rows.Add("2", "Xyz", "200", "2")
dt.Rows.Add("3", "Pqr", "300", "3")
dt.Rows.Add("4", "Lmn", "400", "4")
dt.Rows.Add("5", "Stu", "500", "10")
ViewState("dt") = dt
Me.BindOrders()
End If
End Sub
Private Sub BindOrders()
Me.gvOrders.DataSource = TryCast(ViewState("dt"), DataTable)
Me.gvOrders.DataBind()
End Sub
Protected Sub OnDeleteRow(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = New DataTable()
dt = TryCast(ViewState("dt"), DataTable)
Dim row As GridViewRow = gvOrders.SelectedRow
Dim index As Integer = row.RowIndex
If ViewState("dt") IsNot Nothing Then
dt.Rows(index).Delete()
End If
Me.BindOrders()
End Sub
Screenshot