获得总数据行asp.net的 [英] Get total of a data row asp.net

查看:84
本文介绍了获得总数据行asp.net的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林试图获得在一行中从每一列中的值等于总。这里是code,即时通讯使用C#asp.net实现这种

Im trying to get the values from each column in a single row to equal a total. Here is the code that im using to achieve this in c# asp.net

DataTable dt = ds.Tables.Add("InspireTable");
        string pass = (String)Session["name"];
        if (pass.Equals("High"))
        {
            dt.Columns.Add("Inspire", typeof(string));
            dt.Columns.Add("SNS", typeof(int));
            dt.Columns.Add("TT", typeof(int));
            dt.Columns.Add("Music", typeof(int));
            dt.Columns.Add("Total", typeof(string));


            DataRow row = dt.NewRow();
            row["Inspire"] = "Score";
            row["SNS"] = 10;
            row["TT"] = 10;
            row["Music"] = 0;

            dt.Rows.Add(row);
            Chart1.DataSource = dt;
            this.GridView1.Visible = true;
            GridView1.DataSource = dt;
            GridView1.DataBind();

        }

任何想法?我已经打过电话的每一列和添加他们,但似乎没有工作。

Any ideas? I have tried calling each column and adding them but that seem not to work.

推荐答案

通过老的 DataTable.Compute 方法,例如:

With the old DataTable.Compute method, for example:

int snsTotal = (int) dt.Compute("SUM(SNS)", null); // the second argument is the filter

这里的现代 的LINQ 办法:

int snsTotal = dt.AsEnumerable().Sum(r => r.Field<int>("SNS"));

修改:它好像要每行的数值概括成一个总-column。然后,你可以使用列-Ex的pression:

Edit: it seems as if you want to sum the numeric values of each row into a "total"-column. Then you can use a Column-Expression:

dt.Columns.Add("Total", typeof(int), "SNS + TT + Music");

这篇关于获得总数据行asp.net的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆