using .Rows.Add for a datatable is very slow

G

Gerry Viator

Hi all,

Problem adding the first new row in a datatable, very slow when
it gets to this line "MainSavedDataTable.Rows.Add(myRow)"

see below.

I have a datatable called "MainSavedDataTable" declared in Module

I then call this in a procedure to load Columns, which is no problem

Friend Sub CreateValueTable()
Dim i As Integer
Dim CntColumn As New DataColumn("Count")
CntColumn.DataType = GetType(Integer)
CntColumn.AutoIncrement = True
CntColumn.AutoIncrementSeed = 1
MainSavedDataTable.Columns.Add(CntColumn)

With MainDS.Tables("EntryDataTable")
For i = 0 To .Rows.Count - 1
If .Rows(i).Item("ModuleOwner") = GlobalModuleName Then
Dim StrName As String = .Rows(i).Item("EntryName")
Dim fusename As String = "Col" & StrName
Dim Colname As New DataColumn(StrName)
Colname.DataType = GetType(String)
MainSavedDataTable.Columns.Add(Colname)

End If
Next
End With
'Make datagrid equal datatable
EntryGrid.DataSource = MainSavedDataTable

End Sub

'sEntryName = the correct Column to place the value

'Now add first value to the "sEntryName" Column

Dim myRow As DataRow = MainSavedDataTable.NewRow
ItemValue = txtboxforlist.Text
myRow(sEntryName) = ItemValue
MainSavedDataTable.Rows.Add(myRow)
MessageBox.Show("Value Added")


Again the problem is when I add the first row in the datatable
(MainSavedDataTable.Rows.Add(myRow)), takes 3 - 4 seconds before the
messagebox pops up.
I'm actualy updating a datagrid but, used a messagebox to just help me to
determine where it is slow.

thanks for your help
Gerry
 
M

Miha Markic [MVP C#]

Hi Gerry,

It shouldn't be that slow. Is your table bound to any control?
Better way of using MessageBox is using TimeSpan or a profiler to measure
the bottleneck.
 
G

Gerry Viator

Hi,
thanks for your help.

No, it's not bound. What's strange is when I launch the exe file of my
application from the bin directory, it's fast, or using a
profiler. Seems to only be slow in VS.NET 2003 interface when I run it and
only at that point?

thanks
Gerry




Miha Markic said:
Hi Gerry,

It shouldn't be that slow. Is your table bound to any control?
Better way of using MessageBox is using TimeSpan or a profiler to measure
the bottleneck.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Gerry Viator said:
Hi all,

Problem adding the first new row in a datatable, very slow when
it gets to this line "MainSavedDataTable.Rows.Add(myRow)"

see below.

I have a datatable called "MainSavedDataTable" declared in Module

I then call this in a procedure to load Columns, which is no problem

Friend Sub CreateValueTable()
Dim i As Integer
Dim CntColumn As New DataColumn("Count")
CntColumn.DataType = GetType(Integer)
CntColumn.AutoIncrement = True
CntColumn.AutoIncrementSeed = 1
MainSavedDataTable.Columns.Add(CntColumn)

With MainDS.Tables("EntryDataTable")
For i = 0 To .Rows.Count - 1
If .Rows(i).Item("ModuleOwner") = GlobalModuleName Then
Dim StrName As String = .Rows(i).Item("EntryName")
Dim fusename As String = "Col" & StrName
Dim Colname As New DataColumn(StrName)
Colname.DataType = GetType(String)
MainSavedDataTable.Columns.Add(Colname)

End If
Next
End With
'Make datagrid equal datatable
EntryGrid.DataSource = MainSavedDataTable

End Sub

'sEntryName = the correct Column to place the value

'Now add first value to the "sEntryName" Column

Dim myRow As DataRow = MainSavedDataTable.NewRow
ItemValue = txtboxforlist.Text
myRow(sEntryName) = ItemValue
MainSavedDataTable.Rows.Add(myRow)
MessageBox.Show("Value Added")


Again the problem is when I add the first row in the datatable
(MainSavedDataTable.Rows.Add(myRow)), takes 3 - 4 seconds before the
messagebox pops up.
I'm actualy updating a datagrid but, used a messagebox to just help me to
determine where it is slow.

thanks for your help
Gerry
 
K

Kevin Yu [MSFT]

Thanks for Miha's quick response.

Hi Gerry,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that it is very slow if you run your
application from VS.NET IDE when trying to add the first row to a
DataTable. If there is any misunderstanding, please feel free to let me
know.

As far as I know, this is common. Because when running the application from
VS.NET, the program is running in debug mode, the IDE debugger is attached
to the process. When a line of code is executed, the debugger will perform
some extra tasks, such as check the breakpoints and refresh the watch
variables. So please don't worry if the application runs well from the bin
directory.

HTH. If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Gerry Viator

Thanks,

I just changed some of the lines around and now it's fine.

thanks
Gerry
 
K

Kevin Yu [MSFT]

Hi Gerry,

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

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

Top