正在做的是披萨菜单,我有3个dataGridView,第1个显示菜单,第2个显示所选披萨的成分,第3个显示成分列表(用户可以添加删除成分)。问题是我需要通过自动更新第二个DGV来计算成分的价格,然后在第一个DGV中显示最终价格
pizzaGrid.DataSource = SelectedList;
DataGridViewCell pizzacell = MenuGrid.CurrentCell;
DataGridViewCell ingrecell = IngredientCell.Currentcell;
int total = 0;
for(int i = 0; i < ingredientList.Count; i++)
{
Ingredient ingre = ingredientList[i];
if(ingredientList.Contain(i))
{
total++;
SelectedList.Add(i);
}
}
答案 0 :(得分:0)
如果我理解你的问题是正确的,你想从数据网格中计算一列中所有值的总价,你可以试试这样的事情
int sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value);
}
label1.text = sum.ToString();
答案 1 :(得分:0)
你应该构建一个看起来像这样的方法:
private double calculatePrice()
{
double sum = 0; //Double, because we are dealing with money, which is a decimal
for(int i = 0; i < IngredientGrid.Rows.Count; i++)
{
sum += Convert.ToDouble(IngredientGrid.Rows[i].Cells[1].Value); //This takes the value of your selected cell within your ingredients datagrid (they need to be only numbers, no letters since we're converting to a double)
}
return sum;
}
然后你调用这个方法,例如改变索引或者像这样:
label1.Text = calculatePrice().ToString();