delete multiple row in datatable

AMER SAID 396 Reputation points
2021-11-04T07:18:34.21+00:00

hi

how to delete multiple row in datatable?
I want to delete a number of rows from the datatable, for example, from row 10- to row 35.
And save the changes to the datatable.

Developer technologies VB
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-11-04T10:33:02.463+00:00

    Use Take and Skip to get the data rows to remove then use a for/next to remove the rows. Here I delete the first three rows.

    Dim table As New DataTable()  
    table.Columns.Add("SomeColumn", GetType(Integer))  
    For index As Integer = 0 To 9  
        table.Rows.Add(index)  
    Next  
      
      
    Console.WriteLine(table.Rows.Count)  
      
    Dim rows = table.AsEnumerable().Take(3)  
      
    For index As Integer = 0 To rows.Count() - 1  
        rows(index).Delete()  
    Next  
      
    Console.WriteLine(table.Rows.Count)  
    
    0 comments No comments

  2. Xingyu Zhao-MSFT 5,381 Reputation points
    2021-11-11T03:26:42.593+00:00

    Hi @AMER SAID ,

    I want to delete a number of rows from the datatable, for example, from row 10- to row 35.

    You can use the following code to do it(code from here).

            Dim rowsToDelete As List(Of DataRow) = New List(Of DataRow)()  
            Dim num As Integer  
            For Each row As DataRow In dt.Rows  
                If num >= 10 And num <= 35 Then  
                    rowsToDelete.Add(row)  
                End If  
                num += 1  
            Next  
            For Each row As DataRow In rowsToDelete  
                dt.Rows.Remove(row)  
            Next  
    

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.