Delete DataTable Row Error: Collection was modified enumeration operation might not execute

Last Reply 25 days ago By arunkurmi

Posted 26 days ago

row deleting from gridview error is coming 

Collection was modified enumeration operation might not execute.

        private void insertintodisgrid()
        {
            DataTable dtp = ViewState["dtp"] as DataTable;
            for (int i = dtp.Rows.Count - 1; i >= 0; i--)
            {
                DataRow dr = dtp.Rows[i];
                if (dr["Prdno"].ToString() == txtno.Text)
                {
                    dr.Delete();
                    Bind();
                }

                insertintopacktxt();
            }
        }

 

 

Posted 26 days ago
Hi @akhter,
Please try the following

Removing columns from DataSet Error: collection was modified enumeration operation may not execute

It might help you.

Cheers Andrea.

Posted 25 days ago

Hi Andrea,

i am not getting clear as per your ref link..


Posted 25 days ago

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