可以用DataColumn.Expression 来实现前几个数据的和或公式的运算

解决方案 »

  1.   

    不太懂你的意思,你把数据和公式取出再运算不行,为什么非要用SQL来实现?
      

  2.   

    示例代码:
    DataColumn cPrice;
    DataColumn cTax;
    DataColumn cTotal;
    DataTable myTable = new DataTable();//Create the first column.
    cPrice = new DataColumn();
    cPrice.DataType = Type.GetType("System.Decimal");
    cPrice.ColumnName = "price";
    cPrice.DefaultValue = 50;//Create the second,calculated, column.
    cTax = new DataColumn();
    cTax.DataType = Type.GetType("System.Decimal");
    cTax.ColumnName = "Tax";
    cTax.Expression = "price*0.2";//Create the third column
    cTotal = new DataColumn();
    cTotal.DataType = Type.GetType("System.Decimal");
    cTotal.Expression = "price + tax ";myTable.Columns.Add(cPrice);
    myTable.Columns.Add(cTax);
    myTable.Columns.Add(cTotal);
      

  3.   

    你要在dataTable中加几个dataColumn,设置dataColumn的Expression
    比如有表中已经有这样两列unitPrice和quantity,你想求总价,那新加入dataColumn的Expression就可以设置为"unitPrice * quantity"
      

  4.   

    显示数据之前提出需要计算的数据在C#里面计算,计算后再添加到datagrid中去也可以,这种方法比较好理解了,^_^
      

  5.   

    用DataColumn.Expression==XXX
    但是一般我都是计算好后才写入表格
      

  6.   

    你在生成dataset的时候手动增加也可以,在调用时通过数据库的查询来计算,直接绑定就行了!
      

  7.   

    你修改SqlDataAdapter的selectcommand的SQL查询语句呢?
      

  8.   

    你可以让SQLServer完成你的数据统计啊,效率要高很多!如:
    select sum(money) 'SUM Money' from orders