VB.NET 改變datatable數據類型


Public Shared Function modifyDataTableType(ByVal dt As DataTable, ByVal list As List(Of String), Optional ByVal dectype As Boolean = True) As DataTable
    Dim newDt As DataTable = dt.Clone
    For i As Integer = 0 To list.Count - 1
        If dectype Then
            newDt.Columns(list(i)).DataType = GetType(Decimal)
        Else
            newDt.Columns(list(i)).DataType = GetType(String)
        End If
    Next

    For a As Integer = 0 To dt.Rows.Count - 1
        Dim dr As DataRow = newDt.NewRow
        For b As Integer = 0 To dt.Columns.Count - 1
            dr(b) = dt.Rows(a)(b)
        Next
        newDt.Rows.Add(dr)
    Next
    Return newDt
End Function

調用如下:

Dim colNm As String() = {"salesprice", "purchase", "profit", "salesdateys", "profitys"}  //聲明需要修改的列名 數組

Dim newDt As DataTable = Utils.Util.modifyDataTableType(csvDt, colNm.ToList)  //調用上面方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM