How to add multiple rows

3.3k views Asked by At

As the question says, how can I add multiple rows to a datatable in vb.net? I mean, without looping. F. e. I have an array of datarows and I can then write: dt.Rows.Add(rowarray).

Is this possible? I have searched the net, but found everywhere just looping (or I used the wrong search words).

1

There are 1 answers

0
Mark On BEST ANSWER

I don't think it's possible - DataRowCollection doesn't seem to have anything like AddRange to hide the looping, although I suppose you could write your own extension method:

Imports System.Runtime.CompilerServices

Module MyExtensions

    <Extension>
    Public Sub AddRange(existing As DataRowCollection, newRows As IEnumerable(Of DataRow))
        For Each row In newRows
            existing.Add(row)
        Next
    End Sub

End Module

And use that in your code:

dt.Rows.AddRange(rowarray)