小兰爱学习

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)  //调用上面方法

posted on 2019-08-09 20:09  小兰爱学习  阅读(1927)  评论(0编辑  收藏  举报

导航