【问题标题】:Sum rows in datagridview set label = the amountdatagridview set label中的总和行=数量
【发布时间】:2020-10-07 03:47:26
【问题描述】:

我是一名正在开发的新学生,我正在寻找可以帮助我在 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();
    }

【问题讨论】:

  • 您的问题不清楚...您面临的问题是什么?你有按钮吗?您需要帮助来获取选定的行吗?
  • 我的问题需要在表单上添加标签以显示总行数
  • 您可以像这样计算 onclick 中的总和:dataGridView.Rows.Cast()。 Sum(row => Convert.ToDecimal(row.Cells[dataGridView1.Columns["Amount"].Index].Value));

标签: c# .net winforms


【解决方案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

希望对你有帮助

【讨论】:

  • tnks 4yr help 我使用了这个代码,它给了我想要的东西 现在,我需要为(小计和增值税)添加 2 个标签,那么代码应该是什么? dataGridView1_CellEndEdit() { foreach (dataGridView1.Rows 中的DataGridViewRow 行) {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));} int sum = 0; for (int i = 0; i
  • 我注意到你没有使用绑定选项 - 所以假设它们都是从总和计算的,你可以这样做(将百分比更改为那里的任何值): const int VAT_PERCENTAGE = 17;小数增值税 = (总金额 * VAT_PERCENTAGE / 100); lblVat.Text = vat.ToString(); lblSubtotal.Text = (sum + vat).ToString();
【解决方案2】:

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

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;
    }
}

【讨论】:

  • tnks 4yr help 我使用了这个代码,它给了我想要的东西 现在,我需要为(小计和增值税)添加 2 个标签,那么代码应该是什么? dataGridView1_CellEndEdit() { foreach (dataGridView1.Rows 中的DataGridViewRow 行) {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));} int sum = 0; for (int i = 0; i
  • ex: Qtty = 2 and PurchasePrice = 5 所以,lbl of Sub total = 8.70 lbl of VAT = 1.30 lbl of Total/Sum = 10 所以,代码应该如何?
  • @user14305004 标签需要为整个表格或每一行显示此信息?
  • @audzzy 如何计算增值税,如下例所示 购买价格 100 含增值税 15% 因此,小计应为 = 86.96 增值税 = 13.04 总计 = 100 所以在这种情况下,增值税包含在购买中价格
  • @user14305004 请查看添加了小计和增值税的答案编辑。
【解决方案3】:

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { foreach(dataGridView1.Rows 中的DataGridViewRow 行)

        {
            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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 2017-04-29
    相关资源
    最近更新 更多