1

I need simple condition to replace this (incomplete) if condition.

// i dont want to write all possible data types
if (col.DataType == typeof(int) || col.DataType == typeof(int64) ... all types)
{
    // i want to do something on numeric columns
    // (convert all numbers to double datatype)
}
else
{
    // string and other non-numbers will remain unchanged
}

I was trying something like this:

col.DataType.IsNumeric()

but there is no such method in that class.

I cant use TryParse() method on data because there is too much data.

Condition must be determined only by DataTable column datatype property.

Is there any simple method to simplify my if?

Kamil
  • 13,363
  • 24
  • 88
  • 183
  • Possible repeated question: http://stackoverflow.com/questions/894263/how-to-identify-if-a-string-is-a-number – Javiere Apr 25 '13 at 11:17
  • @Javiere I dont want to determine if specyfic data/variable is convertable to number. – Kamil Apr 25 '13 at 11:20
  • Before your edition, the question was repeated.Now I still understand you need to know if a type isNumeric, question which has been solved before: http://stackoverflow.com/questions/1749966/c-sharp-how-to-determine-whether-a-type-is-a-number/13179018#13179018 – Javiere Apr 25 '13 at 11:31

3 Answers3

4

You could use the DataType as you did, or make a function to do this (see this Determine if DataColumn is numeric):

public static bool IsNumeric(this DataColumn col) {

  if (col == null)
    return false;

  // all numeric types
  var numericTypes = new [] { typeof(Byte), typeof(Decimal), typeof(Double),
        typeof(Int16), typeof(Int32), typeof(Int64), typeof(SByte),
        typeof(Single), typeof(UInt16), typeof(UInt32), typeof(UInt64)};

  return numericTypes.Contains(col.DataType);
}

And use

if (col.IsNumeric()) 
{
   // the column is numeric
}

Now, for the content of your column, you could try using double.TryParse() method, something like this:

double temp;
if (double.TryParse(col["Column"].ToString(), out temp))
{
  // the content can be converter and you can read it from temp variable.
}
Community
  • 1
  • 1
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
  • So there is no dedicated method for this, and I need list of all possible types. Thanks. – Kamil Apr 25 '13 at 11:25
  • @Kamil i think this [link](http://stackoverflow.com/a/8836054/1993545) provides a more readable solution – WiiMaxx Apr 25 '13 at 11:31
  • Yes, you have to use `DataType` property. You also can check if the type is `double?` (nullable), and sometimes your column can has a null value. – Felipe Oriani Apr 25 '13 at 11:31
1

You can also check DataColumn.DataType.Name property. like :

string[] IntType ={"Byte","Decimal","Double","Int16","Int32","SByte",
                "Single","UInt16","UInt32","UInt64"};

static void Main(string[] args)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("num", typeof(int));
    dt.Columns.Add("str", typeof(string));
    Program p=new Program();
    string type = dt.Columns["num"].DataType.Name;
    if (p.IntType.Contains(type))
    {
        //True
    }

}
Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61
0

You may try this:

if(Microsoft.VisualBasic.Information.IsNumeric
    (
     Activator.CreateInstance(col.DataType
    )
   )
Victor Mukherjee
  • 10,487
  • 16
  • 54
  • 97