在 vb.net 中更改数据表中列的值 [英] changing values of a column in datatable in vb.net

查看:32
本文介绍了在 vb.net 中更改数据表中列的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历数据包并更改该数据表中特定列的值.

I want to loop through a databale and change the values of a specific column in that datatable.

eg:数据库返回Request_IDDescDateStatus_Ind.Status_Ind 列包含整数值.我不想向用户显示整数值.我想根据该列中返回的整数值将其转换为字符串值.

eg: the database returns Request_ID, Desc, Date and Status_Ind. The Status_Ind column contains integer values. I dont want to show the integer values to the user. I want to convert that to a string value based on the integer values returned in that columns.

if Status_Ind = 1 then 
    'the values changes to Published

推荐答案

您可以通过创建另一个列来实现:

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

这篇关于在 vb.net 中更改数据表中列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆