Hi akhter,
Check this sample. now take its reference and correct your code.
HTML
<asp:GridView runat="server" ID="gvCustomers">
</asp:GridView><br />
<asp:TextBox ID="txtName" runat="server" AutoPostBack="true" OnTextChanged="OnNameDelete" />
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("ID"),
new DataColumn("Name")
});
dt.Rows.Add("1", "Smith");
dt.Rows.Add("2", "John");
dt.Rows.Add("3", "Rachel");
dt.Rows.Add("4", "Rohit");
dt.Rows.Add("5", "Rohan");
ViewState["dt"] = dt;
this.BindCustomers();
}
}
private void BindCustomers()
{
this.gvCustomers.DataSource = ViewState["dt"] as DataTable;
this.gvCustomers.DataBind();
}
protected void OnNameDelete(object sender, EventArgs e)
{
DataTable dt = new DataTable();
if (ViewState["dt"] != null)
{
dt = ViewState["dt"] as DataTable;
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i][1].ToString().ToLower() == txtName.Text.Trim().ToLower())
{
dt.Rows.Remove(dt.Rows[i]);
break;
}
}
}
this.BindCustomers();
this.txtName.Text = string.Empty;
}
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("ID"), New DataColumn("Name")})
dt.Rows.Add("1", "Smith")
dt.Rows.Add("2", "John")
dt.Rows.Add("3", "Rachel")
dt.Rows.Add("4", "Rohit")
dt.Rows.Add("5", "Rohan")
ViewState("dt") = dt
Me.BindCustomers()
End If
End Sub
Private Sub BindCustomers()
Me.gvCustomers.DataSource = TryCast(ViewState("dt"), DataTable)
Me.gvCustomers.DataBind()
End Sub
Protected Sub OnNameDelete(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = New DataTable()
If ViewState("dt") IsNot Nothing Then
dt = TryCast(ViewState("dt"), DataTable)
For i As Integer = 0 To dt.Rows.Count - 1
If dt.Rows(i)(1).ToString().ToLower() = txtName.Text.Trim().ToLower() Then
dt.Rows.Remove(dt.Rows(i))
Exit For
End If
Next
End If
Me.BindCustomers()
Me.txtName.Text = String.Empty
End Sub
Screenshot