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