【发布时间】:2019-09-16 16:53:30
【问题描述】:
我有一个超过 200,000 行的 excel 文件。如果我使用 OleDbConnection 将其读入数据表或 ObservableCollection,然后在数据网格中显示,则需要一分钟以上。我将 xlsx 更改为制表符分隔的 txt。我发现将整个 txt 文件放入数据表中非常快。它需要不到 10 秒。如果我将整个 txt 直接放入 ObservableCollection,我想知道该怎么做。它不能逐行完成,因为它会再次花费一分钟以上。
'Define Product property
Public Class Product
Public Property Model As String
Public Property Opt As String
Public Property Description As String
Public Property Price As String
End Class
'define Products as ObservableCollection
Dim Products As New ObservableCollection(Of Product)()
'Read txt file to datatable
Private Sub LoadBound(ByVal fName As String)
Dim textLine As String = String.Empty
Dim splitLine As String()
CSVdata.Columns.AddRange({New DataColumn("Model"),
New DataColumn("Opt"),
New DataColumn("Description"),
New DataColumn("Price")})
CSVdata.Rows.Clear()
If System.IO.File.Exists(fName) Then
Dim objReader As System.IO.StreamReader = New System.IO.StreamReader(fName)
Dim contents = objReader.ReadToEnd()
Dim strReader = New System.IO.StringReader(contents)
Do
textLine = strReader.ReadLine()
If textLine.Contains("""") Then
textLine = textLine.Replace("""", "")
End If
If textLine <> String.Empty Then
splitLine = textLine.Split(vbTab)
If splitLine(0) <> String.Empty OrElse splitLine(1) <> String.Empty Then
CSVdata.Rows.Add(splitLine)
End If
End If
Loop While strReader.Peek() <> -1
End If
End Sub
【问题讨论】:
-
你已经有一个可观察的数据表集合。只需绑定到 DataTable 或将 DataSource 设为 DataTable。
-
这个循环没有开始。
Loop While strReader.Peek() <> -1 -
对不起。我想让它变得简单,忘记粘贴循环的开头。我加了。
标签: c# vb.net datatable datagrid observablecollection