如何计算asp.net中数据表列的总和?

发布于 2024-11-05 19:00:14 字数 146 浏览 1 评论 0 原文

我有一个包含 5 列的数据表:

  • ID
  • 名称
  • 帐号
  • 分行
  • 金额

数据表包含 5 行。

如何将标签控件中金额列的总和显示为“总金额”?

I have a DataTable which has 5 columns:

  • ID
  • Name
  • Account Number
  • Branch
  • Amount

The DataTable contains 5 rows.

How can I show the sum of the Amount Column in a Label Control as "Total Amount"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

独﹏钓一江月 2024-11-12 19:00:14

要计算数据表中列的总和,请使用 数据表.Compute 方法。

链接的 MSDN 文章中的用法示例:

DataTable table = dataSet.Tables["YourTableName"];

// Declare an object variable.
object sumObject;
sumObject = table.Compute("Sum(Amount)", string.Empty);

在“总金额标签”中显示结果,如下所示:

lblTotalAmount.Text = sumObject.ToString();

To calculate the sum of a column in a DataTable use the DataTable.Compute method.

Example of usage from the linked MSDN article:

DataTable table = dataSet.Tables["YourTableName"];

// Declare an object variable.
object sumObject;
sumObject = table.Compute("Sum(Amount)", string.Empty);

Display the result in your Total Amount Label like so:

lblTotalAmount.Text = sumObject.ToString();
厌倦 2024-11-12 19:00:14
 this.LabelControl.Text = datatable.AsEnumerable()
    .Sum(x => x.Field<int>("Amount"))
    .ToString();

如果你想过滤结果:

 this.LabelControl.Text = datatable.AsEnumerable()
    .Where(y => y.Field<string>("SomeCol") != "foo")
    .Sum(x => x.Field<int>("MyColumn") )
    .ToString();
 this.LabelControl.Text = datatable.AsEnumerable()
    .Sum(x => x.Field<int>("Amount"))
    .ToString();

If you want to filter the results:

 this.LabelControl.Text = datatable.AsEnumerable()
    .Where(y => y.Field<string>("SomeCol") != "foo")
    .Sum(x => x.Field<int>("MyColumn") )
    .ToString();
手心的海 2024-11-12 19:00:14

你可以像..

DataRow[] dr = dtbl.Select("SUM(Amount)");
txtTotalAmount.Text = Convert.ToString(dr[0]);

You can do like..

DataRow[] dr = dtbl.Select("SUM(Amount)");
txtTotalAmount.Text = Convert.ToString(dr[0]);
追星践月 2024-11-12 19:00:14

如果你有一个 ADO.Net DataTable 你可以做

int sum = 0;
foreach(DataRow dr in dataTable.Rows)
{
   sum += Convert.ToInt32(dr["Amount"]);
}

如果你想查询数据库表,你可以使用

Select Sum(Amount) From DataTable

If you have a ADO.Net DataTable you could do

int sum = 0;
foreach(DataRow dr in dataTable.Rows)
{
   sum += Convert.ToInt32(dr["Amount"]);
}

If you want to query the database table, you could use

Select Sum(Amount) From DataTable
葬花如无物 2024-11-12 19:00:14

计算数据表中列的总和,100%工作

lbl_TotaAmt.Text = MyDataTable.Compute("Sum(BalAmt)", "").ToString();

如果你想要有任何条件,像这样使用它

   lbl_TotaAmt.Text = MyDataTable.Compute("Sum(BalAmt)", "srno=1 or srno in(1,2)").ToString();

Compute Sum of Column in Datatable , Works 100%

lbl_TotaAmt.Text = MyDataTable.Compute("Sum(BalAmt)", "").ToString();

if you want to have any conditions, use it like this

   lbl_TotaAmt.Text = MyDataTable.Compute("Sum(BalAmt)", "srno=1 or srno in(1,2)").ToString();
始终不够 2024-11-12 19:00:14
  public decimal Total()
    {
      decimal decTotal=(datagridview1.DataSource as DataTable).Compute("Sum(FieldName)","");
      return decTotal;
    }
  public decimal Total()
    {
      decimal decTotal=(datagridview1.DataSource as DataTable).Compute("Sum(FieldName)","");
      return decTotal;
    }
层林尽染 2024-11-12 19:00:14

名称分组来使用Linq。

  var allEntries = from r in dt.AsEnumerable()
                            select r["Amount"];

您可以使用命名空间using System.Linq;通过

您可以使用c#在我的博客

You Can use Linq by Name Grouping

  var allEntries = from r in dt.AsEnumerable()
                            select r["Amount"];

using name space using System.Linq;

You can find the sample total,subtotal,grand total in datatable using c# at Myblog

哑剧 2024-11-12 19:00:14

试试这个

int sum = 0;
foreach (DataRow dr in dt.Rows)
{
     dynamic value = dr[index].ToString();
     if (!string.IsNullOrEmpty(value))
     { 
         sum += Convert.ToInt32(value);
     }
}

Try this

int sum = 0;
foreach (DataRow dr in dt.Rows)
{
     dynamic value = dr[index].ToString();
     if (!string.IsNullOrEmpty(value))
     { 
         sum += Convert.ToInt32(value);
     }
}
尐籹人 2024-11-12 19:00:14

我认为这解决了

using System.Linq;


(datagridview1.DataSource as DataTable).AsEnumerable().Sum(c => c.Field<double>("valor"))

I think this solves

using System.Linq;


(datagridview1.DataSource as DataTable).AsEnumerable().Sum(c => c.Field<double>("valor"))
别想她 2024-11-12 19:00:14

如果您想在 cshtml 文件中执行此操作,您可以这样编写(包括 LAMBDA 表达式):

<td><b>£@Model.Sum(i => i.Amount)</b></td>

您可以删除 html 标签,我只是将它们留在其中以尝试帮助完成示例。

If you're wanting to do this within your cshtml file, you can write it like this (including a LAMBDA expression):

<td><b>£@Model.Sum(i => i.Amount)</b></td>

You can remove the html tags, I just left them in to try and help with the example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文