如何根据VB中数据表中的现有列添加新列?

时间:2016-09-14 06:18:33

标签: vb.net visual-studio-2010

我正在尝试构建一个代码,用于将指纹机中的文本文件处理为报告。

文本文件输入示例,

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'可以写入新列。

提前致谢。

1 个答案:

答案 0 :(得分:0)

您已创建自己的DataTable,因此您需要先添加一个额外的列

For i As Int32 = 1 To colCount + 1
    'Dim header As String
    tblabsen.Columns.Add(New DataColumn("Column_" & i, GetType(String)))
Next

之后,您需要在ForEach

中指定您的值
For Each line In lines
    Dim objFields = line.Split(";"c).ToList()
    Dim newRow = tblabsen.Rows.Add()

    Dim kete As String
    Select Case objField(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
    objFields.Add(kete)

    newRow.ItemArray = objFields.ToArray()
Next

此外,您还需要在其余代码中处理此额外列(即调整大小,列名称)