Search code examples
c#.netvb.netlinqsystem.data.datatable

.net: DataTable setting all non numeric values of data table as NULL


Need help to format datatable in .net, giving code below in vb.net

Dim dt As New DataTable
            dt.Columns.Add("a", GetType(System.String))
            dt.Columns.Add("b", GetType(System.String))

            dt.Rows.Add("N/A", "22")
            dt.Rows.Add("99", "22.3")
            dt.Rows.Add("Not Available", "#Error")
            dt.Rows.Add("5.5", "10")

' Need Suggestions to transform data table in .net c#/vb

'Convert all Non Numeric Values of Data Table to NULL, trying to avoid Looping and updating each row by using regex

            'Result dt
            'a      b       
            'null   22
            '99     22.3
            'null   null
            '5.5    10

Solution

  • Hm if u don't need the table to contain the non-numeric value to begin with and don't get it handed like this, u can write your own method to add rows, which checks before adding them if they are numeric or not.

    Otherwise i can't think of anything preventing a loop, if you don't want to use regex for some reasons you can check if a convertion is possible (Convert.ToDouble)