1

我是一名正在开发的新学生,我正在寻找可以帮助我在 datagridview 中的行总和时更改标签的 C# 代码

例如:我正在使用此代码来计算 datagridview 中的特定行

foreach (DataGridViewRow row in dataGridView1.Rows)
{
   row.Cells[dataGridView1.Columns["Amount"].Index].Value = 
           (Convert.ToDecimal(row.Cells[dataGridView1.Columns["Qtty"].Index].Value) * 
           Convert.ToDecimal(row.Cells[dataGridView1.Columns["PurchasePrice"].Index].Value));
}

我想要做的是:在我填写PurchasePriceqtty计算金额之后,当我按下回车按钮时,标签应该是 = 中的相同金额datagridview

谢谢

编辑:

    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)

        {
            row.Cells[dataGridView1.Columns["Amount"].Index].Value = (Convert.ToDecimal(row.Cells[dataGridView1.Columns["Qtty"].Index].Value) * Convert.ToDecimal(row.Cells[dataGridView1.Columns["PurchasePrice"].Index].Value));
        }

        //datagridview column total in label
        decimal sum = 0;
        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            sum += Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value);
        }

        lblSum.Text = sum.ToString();


        //vat
        decimal vat = (sum * 15 / 100);
        lblVat.Text = vat.ToString();

        //subtotal 
        decimal subtotal = (sum - vat);
        lblSubtotal.Text = subtotal.ToString();
    }
4

3 回答 3

1

假设列都已满并且该部分工作正常,并且您现在想要的是有一个显示金额总和的标签,您可以执行以下操作:

    decimal SumAmounts = 0;
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
       decimal amount = (Convert.ToDecimal(row.Cells[dataGridView1.Columns["Qtty"].Index].Value) * 
                        (Convert.ToDecimal(row.Cells[dataGridView1.Columns["PurchasePrice"].Index].Value));
       row.Cells[dataGridView1.Columns["Amount"].Index].Value = amount;
       SumAmounts += amount;
    }
    lblSum.Text = sumAmounts;

但您也可以将总和保存为属性并将标签绑定到它,以便标签始终正确..

为此,您需要在您的类中实现 INotifyPropertyChanged 接口,并拥有一个在其更改时通知的属性:

private decimal sumAmount;
public decimal SumAmount { 
    get { return sumAmount; }
    set 
    { 
        sumAmount = value;
        if(PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs("SumAmount"));
        }
    }
}

然后在 InitilizeComponents 之后将标签绑定到 ctor 中的值:

lblSum.DataBindings.Add(new Binding("Text", this, "SumAmount");

您设置数据的代码如下所示:

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
       decimal amount = (Convert.ToDecimal(row.Cells[dataGridView1.Columns["Qtty"].Index].Value) * 
                        (Convert.ToDecimal(row.Cells[dataGridView1.Columns["PurchasePrice"].Index].Value));
       row.Cells[dataGridView1.Columns["Amount"].Index].Value = amount;
       SumAmounts += amount;
    }

现在,每当您更改 SumAmounts - 标签将自动更新。

您可以在此处阅读有关 dataBinding 的更多信息: https ://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial

我希望这有帮助

于 2020-10-04T22:52:02.963 回答
0

如果我理解正确 - 用户在“Qtty”和“PurchasePrice”列中输入数据,“Amount”列将显示“Qtty”*“PurchasePrice”。如果是这种情况,则使用CellEndEdit 事件并检查两列是否包含正确格式的数据,如下所示:

const double VAT_PERCENT = 15; // Added for VAT calculation
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    var isQty = int.TryParse(
        dataGridView1.Rows[e.RowIndex].Cells["Qtty"].Value?.ToString().Trim(),
        out int qty);

    var isPrice = int.TryParse(
        dataGridView1.Rows[e.RowIndex].Cells["PurchasePrice"].Value?.ToString().Trim(),
        out int price);
    
    // Added SubTotal and VAT calculations    
    if (isQty && isPrice)
    {
        double percent = VAT_PERCENT / 100;
        double subtotal = qty * price / (1 + percent);
        double vat = qty * price - subtotal;
        int amount = qty * price;
        dataGridView1.Rows[e.RowIndex].Cells["Amount"].Value = amount;
        dataGridView1.Rows[e.RowIndex].Cells["SubTotal"].Value = subtotal;
        dataGridView1.Rows[e.RowIndex].Cells["VAT"].Value = vat;
    }
}
于 2020-10-04T22:38:31.403 回答
0

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { foreach (DataGridViewRow row in dataGridView1.Rows)

        {
            row.Cells[dataGridView1.Columns["Amount"].Index].Value = (Convert.ToDecimal(row.Cells[dataGridView1.Columns["Qtty"].Index].Value) * Convert.ToDecimal(row.Cells[dataGridView1.Columns["PurchasePrice"].Index].Value));
        }

        //datagridview column total in label
        decimal sum = 0;
        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            sum += Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value);
        }

        lblSum.Text = sum.ToString();

        const double VAT_PERCENT = 15;
        double percent = VAT_PERCENT / 100;
        double subtotal = Qtty * [price error here] / (1 + percent);
        double vat = Qtty * [price error here] - subtotal;
}
于 2020-10-06T13:28:36.557 回答