DataTable Add Row is slow (especially with empty table)

  • Thread starter Thread starter diesel
  • Start date Start date
D

diesel

After several frustrating hours, I found a solution to a problem that
I've been having with DataTable.Add running very slow when trying to
add a bunch of records. I found similar questions posted but no good
answers, so I thought I'd post this in hopes of helping someone else.

Problem: DataTable.Add is very slow. It may be relatively fast when
the DataTable already has some rows in it, but is terribly slow when
the DataTable is empty.

First, unbind any controls that are bound to the DataTable or dependent
DataViews. For example: myDataGrid.SetDataBinding(Nothing, Nothing).

Next, before you start your batch Add process, call
myDataTable.BeginLoadData(). This turns off notifications that the
DataTable normally does automatically when you Add. After you are done
adding rows, call myDataTable.EndLoadData(). This turns them back on.

myDataGrid.SetDataBinding(Nothing, Nothing)
myDataTable.BeginLoadData()
' Add a bunch of rows here...
myDataTable.EndLoadData()

Good luck.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top