Delete A Particular DataRow

retkehing

Well-known member
Joined
Feb 5, 2006
Messages
153
Programming Experience
Beginner
How to delete a particular DataRow when it meets a condition? The following code will check through the Row but i don't know how to specify the current row, which is needed to be deleted.

VB.NET:
Dim DRow As DataRow
For Each DRow In Employee.Rows
If (DRow("emp_no") = "1")  Then
'Delete the row contains value 1
End If
Next
 
Is there any alternative i want the For...loop continues until end of the record? But once the system delete a particular rows, it will return an error message. May i know how to solve it? Thank you.
 
For each row As DataRow in Employee.Rows
If row("emp_no") = "1" Then
row.Delete
Exit For
End If
End If

You'll get an exception from that, blathering on about modifying the collection that an enumeration is based on while youre enumerating...
 
Is there any alternative i want the For...loop continues until end of the record? But once the system delete a particular rows, it will return an error message. May i know how to solve it? Thank you.

try:
VB.NET:
Dim rows() as DataRow = Employee.Select("emp_no = '1'")
For i as Integer = 0 to rows.Length -1
  rows(i).Delete
Next i

If emp_no is the PK:

Employee.FindByemp_id(1).Delete
 

Latest posts

Back
Top