如何从给定的DataTable列中删除字符? [英] How to remove character from a given DataTable column?

查看:203
本文介绍了如何从给定的DataTable列中删除字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列 TOTAL_DOLLARS ,其中包含 String 我要转换为的值小数位数。但是,这些值还有一个 $ 字符(因为它们是美元金额)。我想删除这个 $ 字符,虽然我找不到一种方法。

I have a column TOTAL_DOLLARS that holds String values that I want to convert to Decimal. However, these values also have a $ character preceding them (as they are dollar amounts). I want to remove this $ character, though I can't quite find a method to do so.

这里是我正在尝试的:

For Each row As DataRow In myTable.Rows
    Replace(row.Item("TOTAL_DOLLARS"), "$", "")
Next

任何帮助都是

推荐答案

假设您要在TOTAL_DOLLARS列上保留数据完整性,只需添加所需类型的新列并循环填充它:

Assuming you want to preserve data integrity on your "TOTAL_DOLLARS" column, simply add a new column of the desired type and loop through to fill it:

myTable.Columns.Add("Total", GetType(Decimal))
For Each row As DataRow In myTable.Rows
    row("Total") = Convert.ToDecimal(row("TOTAL_DOLLARS").ToString.Replace("$", ""))
Next

或这样(保留NULL):

or like this (to preserve NULLs):

myTable.Columns.Add("Total", GetType(Decimal))
For Each row As DataRow In myTable.Rows
    If Not IsDBNull(row("TOTAL_DOLLARS")) Then
        row("Total") = Convert.ToDecimal(row("TOTAL_DOLLARS").ToString.Replace("$", ""))
    Else
        row("Total") = System.DBNull.Value
    End If
Next

这篇关于如何从给定的DataTable列中删除字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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