How do I use IF/ELSE or CASE In DataColumn.Expression?
Asked Answered
M

2

8

I have a table with 1 column: 'Status' I want to add in another column named 'Action', its value will be as follow: if Status = 'Yes' Then Action = 'Go', otherwise, Action = 'Stop'. I used this following code to add in column 'Action' but it didn't work:

myDataTable.Columns.Add("Action", typeof(string), "IF [Status] = 'Yes' THEN 'Go' ELSE 'Stop' END");
Mehala answered 11/10, 2013 at 18:38 Comment(0)
E
19

The expression you are looking for is:

IIF( [Status] = 'Yes', 'Go', 'Stop' )

DataTables do not support standard SQL CASE statements, nor do they support "IF... ELSE" statements. You must use the inline-if function: IIF

See DataColumn.Expression Property (MSDN)

Elasticity answered 11/10, 2013 at 18:51 Comment(0)
H
0

Don't know since when but now it's supported, u can use IIF function as a column expression (check it here)

Example: myDataColumn.Expression = "**IIF(total>1000, 'expensive', 'dear')**"

Hypomania answered 22/7, 2022 at 9:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.