Deleting row from datatable in C#

Viewed 114694

I'm having a problem deleting rows from a datatable. In my program, I am reading info from a database into a datatable using an sql query. I use an oledb connection and the code dt.Load(command.ExecuteReader()); to do this. Later, I want to delete rows that match an id string. I have tried the following code buy cannot get it to work:

            DataRow[] drr = dt.Select("Student=' " + id + " ' "); 
            for (int i = 0; i < drr.Length; i++)
                dt.Rows.Remove(drr[i]);
            dt.AcceptChanges();

Can anyone please suggest another way with an example?

8 Answers

Advance for loop works better for this case

public void deleteRow(DataRow selectedRow)
        {
            foreach (DataRow  in StudentTable.Rows)
            {
                if (SR[TableColumn.StudentID.ToString()].ToString() == StudentIndex)
                    SR.Delete();
            }

            StudentTable.AcceptChanges();
        }
Related