close
Imports System.IO
Imports System.Data
Public Class Form2
Public Function getCSVtoDataTable(ByVal path As String) As DataTable
Dim dt As New DataTable()
Dim line As String = Nothing
Dim i As Integer = 0
Dim column_j As Integer = -6
Using sr As StreamReader = File.OpenText(path)
line = sr.ReadLine()
Do While line IsNot Nothing
Dim dataArray() As String = line.Split(",")
If dataArray.Length > 0 Then
'----- 為了在第一次產生column :only i=0----
If i = 0 Then
For Each item In dataArray 'N項 就先產生 N個column
dt.Columns.Add(New DataColumn(column_j.ToString))
column_j = column_j + 1
Next item
i += 1
End If
'----- /為了在第一次產生column :only i=0/----
Dim row As DataRow = dt.NewRow() ' 注意 Datarow 必須依附datatable 來新增 row
row.ItemArray = dataArray
dt.Rows.Add(row)
End If
line = sr.ReadLine() ' 對每一行row 處理
Loop
End Using
Return dt
End Function
Private Sub TabPage1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage1.Click
Dim table = New DataTable
Try
table = getCSVtoDataTable("c:\temp\t1.csv")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
DataGridView1.DataSource = table
DataGridView1.AutoResizeColumns()
End Sub
Private Sub TabPage2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage2.Click
Dim table = New DataTable
Try
table = getCSVtoDataTable("c:\temp\t2.csv")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
DataGridView2.DataSource = table
DataGridView2.AutoResizeColumns()
End Sub
End Class
Imports System.Data
Public Class Form2
Public Function getCSVtoDataTable(ByVal path As String) As DataTable
Dim dt As New DataTable()
Dim line As String = Nothing
Dim i As Integer = 0
Dim column_j As Integer = -6
Using sr As StreamReader = File.OpenText(path)
line = sr.ReadLine()
Do While line IsNot Nothing
Dim dataArray() As String = line.Split(",")
If dataArray.Length > 0 Then
'----- 為了在第一次產生column :only i=0----
If i = 0 Then
For Each item In dataArray 'N項 就先產生 N個column
dt.Columns.Add(New DataColumn(column_j.ToString))
column_j = column_j + 1
Next item
i += 1
End If
'----- /為了在第一次產生column :only i=0/----
Dim row As DataRow = dt.NewRow() ' 注意 Datarow 必須依附datatable 來新增 row
row.ItemArray = dataArray
dt.Rows.Add(row)
End If
line = sr.ReadLine() ' 對每一行row 處理
Loop
End Using
Return dt
End Function
Private Sub TabPage1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage1.Click
Dim table = New DataTable
Try
table = getCSVtoDataTable("c:\temp\t1.csv")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
DataGridView1.DataSource = table
DataGridView1.AutoResizeColumns()
End Sub
Private Sub TabPage2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage2.Click
Dim table = New DataTable
Try
table = getCSVtoDataTable("c:\temp\t2.csv")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
DataGridView2.DataSource = table
DataGridView2.AutoResizeColumns()
End Sub
End Class
全站熱搜
留言列表