是否在数据网格视图中显示数据?访问,C#
我想计算总价格并将其显示在数据网格视图中。第四列没有从数据库中检索信息,所以我不知道怎么做。以下是GUI的外观: 这是我到目前为止的代码是否在数据网格视图中显示数据?访问,C#,c#,database,visual-studio-2010,ms-access,C#,Database,Visual Studio 2010,Ms Access,我想计算总价格并将其显示在数据网格视图中。第四列没有从数据库中检索信息,所以我不知道怎么做。以下是GUI的外观: 这是我到目前为止的代码 double colTotal = 0; //set column total to 0 foreach (DataRow currentRow in itemDS.Tables[0].Rows) { // add to
double colTotal = 0; //set column total to 0
foreach (DataRow currentRow in itemDS.Tables[0].Rows)
{
// add to data grid view
invoiceDataGridView.Rows.Add(currentRow[1].ToString(), currentRow[2].ToString(), currentRow[3].ToString());
//Decalare variables for item quantity and price
var itemQuant = Convert.ToDouble(currentRow[1]);
var priceEach = Convert.ToDouble(currentRow[3]);
//Multiply the item quantity by the item price
double subTotal = itemQuant * priceEach;
//do this for each row
colTotal += subTotal;
}
//Display subtotal
subTotalOutput.Text = colTotal.ToString();
//Calculate tax
double tax = colTotal * .073;
//Display total tax
taxOutput.Text = Convert.ToString(tax);
//Add the subtotal and the total tax
double totPrice = colTotal + tax;
//Display total price
totalOutput.Text = Convert.ToString(totPrice);
}
首先,我要说的是,使用DataGridView控件的DataSource属性可以让您的生活更轻松,但要解决您当前设置中的问题,我只需移动一些代码,并按如下方式操作:
double subTotal = itemQuant * priceEach;
invoiceDataGridView.Rows.Add(currentRow[1].ToString(), currentRow[2].ToString(), currentRow[3].ToString(), subTotal.ToString());
我相信您可以在填充网格之前进行小计计算,并在调用的末尾添加小计变量。像这样:
foreach (DataRow currentRow in itemDS.Tables[0].Rows)
{
//Decalare variables for item quantity and price
var itemQuant = Convert.ToDouble(currentRow[1]);
var priceEach = Convert.ToDouble(currentRow[3]);
//Multiply the item quantity by the item price
double subTotal = itemQuant * priceEach;
// add to data grid view
invoiceDataGridView.Rows.Add(currentRow[1].ToString(), currentRow[2].ToString(), currentRow[3].ToString(), subTotal);
//do this for each row
colTotal += subTotal;
}
非常感谢你。一开始我不知道该在哪里加上这个,但我弄明白了。谢谢:)