更新时间:2023-11-14 18:29:46
而不是使用for循环,你可以简单地使用 计算
数据表的方法,并通过计算表达式。例如:
VAR总= double.Parse(yourDataTable.Compute(SUM(列1),)的ToString ());
另外要格式化的总展示后,小数位2位数字,您可以使用这些选项:
的0自定义数字格式字符串:的String.Format({0:0.00},计))
的定点F格式说明:的String.Format({0:F2},计))
I want to show sum of total Price of items. I am facing 2 issues:
.00
to the total priceYou 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:
The "0" Custom Numeric Format String : string.Format("{0:0.00}", total))
The Fixed-Point "F" format Specifier: string.Format("{0:F2}", total))