0
我試圖建立一個代碼來處理從指紋機到報告文本文件。如何在VB中的數據表中添加基於現有列的新列?
文本文件輸入例如,
001;0000000000200004;01-07-2016;15:31;01;01
用分號分隔符,
machinecode;personid;dateDD-MM-YYYY;timeHH:MM;jobid;unused
這是我最後一次成功的代碼
Imports System.IO
Imports System.Windows.Forms
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim lines = IO.File.ReadAllLines("C:\Users\Adventa\Documents\Visual Studio 2010\Projects\AbsensiALU\AbsensiALU\Absen.txt")
Dim tblabsen = New DataTable
Dim colCount = lines.First.Split(";"c).Length
For i As Int32 = 1 To colCount
'Dim header As String
tblabsen.Columns.Add(New DataColumn("Column_" & i, GetType(String)))
Next
For Each line In lines
Dim objFields = From field In line.Split(";"c)
Dim newRow = tblabsen.Rows.Add()
newRow.ItemArray = objFields.ToArray()
Next
Dim table1 As New DataTable
table1 = tblabsen.Copy()
table1.Columns(0).ColumnName = "No. Mesin"
table1.Columns(1).ColumnName = "NIK"
table1.Columns(2).ColumnName = "Tanggal"
table1.Columns(3).ColumnName = "Jam"
table1.Columns(4).ColumnName = "Keterangan"
DataGridView1.DataSource = table1
DataGridView1.AutoResizeColumn(0)
DataGridView1.AutoResizeColumn(1)
DataGridView1.AutoResizeColumn(2)
DataGridView1.AutoResizeColumn(3)
DataGridView1.AutoResizeColumn(4)
DataGridView1.Columns(5).Visible = False
Dim rowData As String = ""
'dt is the name of the data table
For Each row As DataRow In table1.Rows
For Each column As DataColumn In table1.Columns
rowData = rowData & column.ColumnName & "=" & row(column) & " "
Next
rowData = rowData & vbNewLine & vbNewLine
Next
End Sub
Private Sub DataGridView1_CellContentClick_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
End Class
現在我想添加一個柱成它,基於table1.columns(4)配有可能是一個IF/CASE操作者,因此,例如在新的列將是這樣
Dim kete As String
Select Case tblabsen.Columns(4).GetType.ToString()
Case "00"
kete = "Masuk"
Case "01"
kete = "Pergi"
Case "02"
kete = "Kembali"
Case "03"
kete = "Pulang"
Case Else
kete = "Error"
End Select
假定「凱泰」可被寫入到新列。
在此先感謝。
感謝您的答覆亞歷克斯,它確實解決了我添加新列的問題,但噸他新列或'kete'值總是「錯誤」,或者我們可以說SELECT操作符不工作,我不知道它。也許我還必須弄清楚如何提取單個單元格或列以供將來參考。 –
@JoeAdventa你有沒有試過在調試模式下看看你有什麼?我相信我們可以省略'.GetType.ToString()' – Prisoner
我只是刪除'.GetType.ToString()',它的工作。現在,我必須在添加新列變量之前瞭解計算值。非常感謝Alex。 –