Reputation: 305
In, C# I built a datatable connected to a datagrid which has three columns: id1, id2 and sum.
I want to add up id1 and id2 into column "sum", but only if sum is greater than x (x will be supplied from textbox1).
I have this code, but this defaults adding all the columns and doesnt allow for an if/then statement.
column = new DataColumn();
column.DataType = Type.GetType("System.Double");
column.ColumnName = "sum";
table.Columns.Add(column);
table.Columns["sum"].Expression = "[id] * [id2]";
I thought about adding a foreach (Datarow row in table.Rows) (), but that doesnt allow me to create if then statements.
Upvotes: 0
Views: 474
Reputation: 7445
presuming you have the value of x at this point, simply do a case statement in the expression.
Do something like -
table.Columns["sum"].Expression = "CASE WHEN [id] * [id2] > " + x + " THEN [id] * [id2] ELSE NULL END ";
BTW - do you want the product or the sum? The column is named sum, but you are getting the product....just curious.
If you don't have x at the time of the query, you will have to do this in the ItemDataBound event of the DataGrid. Have that sum column be a template column and calculate the value for it in the event.
Upvotes: 0