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

18,387

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)

Share:
18,387
J - C Sharper
Author by

J - C Sharper

Updated on June 15, 2022

Comments

  • J - C Sharper
    J - C Sharper almost 2 years

    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");
    
  • J - C Sharper
    J - C Sharper over 10 years
    Thanks, Really useful.