Search code examples
vb.netdatatableiifdatacolumn

colB.Expression = IIF(colA = stringA, stringB, stringC)


In my DataTable I have two columns that are:

With colA
            .DataType = System.Type.GetType("System.String")
            .ColumnName = "colA"
End With
With colB
            .DataType = System.Type.GetType("System.String")
            .ColumnName = "colB"
End With

I want to add it a .expression to colB, that will test a string value from colA and if True put a string value, and if False put another string value.

One thing like this :

colB.Expression = IIF(colA="toto", "cool", "not cool")

But I have difficulties with the syntax. Every time the evaluation of expression is False.

UPDATE: I try this and this work :

colB.Expression = "IIF([colA] = 'stringA', 'stringB', 'stringC')"

But now, if I want to test an empty value , what is the syntax ?


Solution

  • To test for empty:

    colB.Expression = "IIF([colA] = '', 'stringB', 'stringC')"
    

    To test for null:

    colB.Expression = "IIF([colA] IS NULL, 'stringB', 'stringC')"
    

    To test for null or empty:

    colB.Expression = "IIF([colA] = '' or [colA] IS NULL, 'stringB', 'stringC')"