显示数据表的列值的总和 [英] Show total Sum of values of a Column of a DataTable

查看:202
本文介绍了显示数据表的列值的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示的项目总价格的总和。我面临的2个问题:




  • 它显示我错了总项目的价格

  • 我要添加 .00 来总价



您可以查看问题< A HREF =htt​​p://i.stack.imgur.com/1mvxF.jpg相对=nofollow>图片一个明确的解释。



这是我的代码:

  tDisplay.Text =返回/+回执号:+ Return_Form.setalueforText011; 
label1.Text = Return_Form.setalueforText011;

OleDbConnection的VCON =新的OleDbConnection(@供应商= Microsoft.ACE.OLEDB.12.0;数据源= D:\Restaurant.accdb);
的DataSet DSA =新的DataSet();
DataTable的DT =新的DataTable();
dsa.Tables.Add(DT);

OleDbDataAdapter的DA =新OleDbDataAdapter的();
DA =新OleDbDataAdapter的(选择[列1],[列2],[栏3]从[总]其中[收据否] =+ label1.Text +,VCON);
da.Fill(DT);
//dataGridView1.DataSource = DT;

的for(int i = 0; I< dt.Rows.Count;我++)
{
products.Add(新tblProduct(){产品名称= dt.Rows [我] [列2]的ToString(),productSku,则= Convert.ToDecimal(Math.Round(Convert.ToDecimal(dt.Rows [I] [列1]的ToString())))});
label3.Text = dt.Rows [I] [栏3]的ToString()。
textBox59.Text =RS:+的String.Format({0:},合计);
tblProduct选择=(tblProduct)(listBox60.SelectedItem);
共计+ =(十进制)selected.productPrice;
}
VCON.Close();


解决方案

而不是使用for循环,你可以简单地使用< A HREF =htt​​ps://msdn.microsoft.com/en-us/library/system.data.datatable.compute(v=vs.110).aspx相对=nofollow> 计算 数据表的方法,并通过计算表达式。例如:

  VAR总= double.Parse(yourDataTable.Compute(SUM(列1),)的ToString ()); 



另外要格式化的总展示后,小数位2位数字,您可以使用这些选项:




I want to show sum of total Price of items. I am facing 2 issues:

  • It's showing me wrong total of items price
  • I want to add .00 to the total price

You can check issue in the image for a clear explanation.

Here is my code:

tDisplay.Text = "Return/" + "Receipt No:" + Return_Form.setalueforText011;
label1.Text = Return_Form.setalueforText011;

OleDbConnection VCON = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Restaurant.accdb");
DataSet dsa = new DataSet();
DataTable dt = new DataTable();
dsa.Tables.Add(dt);

OleDbDataAdapter da = new OleDbDataAdapter();
da = new OleDbDataAdapter("SELECT [Column1],[Column2],[Column3] from [Total] Where [Receipt No] =  " + label1.Text + "", VCON);
da.Fill(dt);
//dataGridView1.DataSource = dt;

for (int i = 0; i < dt.Rows.Count; i++)
{
    products.Add(new tblProduct() { productName = dt.Rows[i]["Column2"].ToString(),productPrice = Convert.ToDecimal(Math.Round(Convert.ToDecimal(dt.Rows[i]["Column1"].ToString())))});
    label3.Text = dt.Rows[i]["Column3"].ToString();
    textBox59.Text = "Rs: "+String.Format("{0:}", Total);
    tblProduct selected = (tblProduct)(listBox60.SelectedItem);
    Total += (decimal)selected.productPrice;
}
VCON.Close();

解决方案

Instead of using for loop, You can simply use Compute method of data table and pass an expression to compute. For example:

var total = double.Parse(yourDataTable.Compute("SUM(Column1)", "").ToString());

Also to format total to show 2 digits after decimal place, you can use either of these options:

这篇关于显示数据表的列值的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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