changing values of a column in datatable in vb.net

风格不统一 提交于 2019-12-06 08:53:27

You could do it by creating another column:

Dim dt As New DataTable
dt.Columns.Add(New DataColumn("status_int", GetType(Int32)))
dt.Columns.Add(New DataColumn("status_str", GetType(String)))

'add example row - not publised
Dim newDr0 As DataRow = dt.NewRow
newDr0(0) = 0
dt.Rows.Add(newDr0)

'add example row - publised
Dim newDr1 As DataRow = dt.NewRow
newDr1(0) = 1
dt.Rows.Add(newDr1)

For Each dr As DataRow In dt.Rows
    Select Case dr(0)
        Case 1
            dr(1) = "Published"
        Case Else
            dr(1) = "Not published"
    End Select
Next

For Each dr In dt.Rows
    Console.WriteLine(dr(0).ToString + " " + dr(1).ToString)
Next

Assuming your DataTable is defined as this:

Dim dt As DataTable

First you need to add a new column to your DataTable to hold the Status:

dt.Columns.Add("Status", Type.GetType("System.String"))

Now Loop through the DataTable and update the status column:

For Each dr As DataRow In dt.Rows
    Select Case CInt(dr.Item("Status_Ind"))
        Case 1
            dr.Item("Status") = "Published"
        Case 2
            dr.Item("Status") = "Some other Status"
        Case Else
            dr.Item("Status") = "Unknown"
    End Select
Next

Then you could then remove the integer column:

dt.Columns.Remove("Status_Ind")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!