Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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");
like image 748
J - C Sharper Avatar asked Oct 11 '13 18:10

J - C Sharper


People also ask

What is Datacolumn expression?

An expression to calculate the value of a column, or create an aggregate column. The return type of an expression is determined by the DataType of the column. Attributes.

How do you set a same value to all rows in a particular column in Datatable C#?

var col = table. Columns["Foo"]; foreach(var row in table. Rows) row[col] = value; As an alternative : since this presumably relates to a database, write the TSQL manually to set all the values appropriately (i.e. with a suitable where clause in the TSQL).


1 Answers

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)

like image 196
JDB Avatar answered Sep 19 '22 05:09

JDB