Changing the data type 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

Call as follows:

Dim colNm As String () = { "salesprice", "purchase", "profit", "salesdateys", "profitys"} // declare an array of column name to be modified

Dim newDt As DataTable = Utils.Util.modifyDataTableType (csvDt, colNm.ToList) // call the above process

Guess you like

Origin www.cnblogs.com/xlaxx/p/11329360.html