Datagrid if/then 行计算
在 C# 中,我构建了一个连接到数据网格的数据表,该数据网格具有三列:id1、id2 和 sum。
我想将 id1 和 id2 添加到“sum”列中,但前提是 sum 大于 x(x 将由 textbox1 提供)。
我有这段代码,但默认添加所有列,并且不允许使用 if/then 语句。
column = new DataColumn();
column.DataType = Type.GetType("System.Double");
column.ColumnName = "sum";
table.Columns.Add(column);
table.Columns["sum"].Expression = "[id] * [id2]";
我考虑过添加一个 foreach (Datarow row in table.Rows) (),但这不允许我创建 if then 语句。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设此时您有 x 的值,只需在表达式中执行 case 语句即可。
做类似的事情 -
顺便说一句 - 你想要产品还是总和?该列名为 sum,但您正在得到产品......只是好奇。
如果查询时没有 x,则必须在 DataGrid 的 ItemDataBound 事件中执行此操作。将该总和列作为模板列并在事件中计算它的值。
presuming you have the value of x at this point, simply do a case statement in the expression.
Do something like -
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.