总和DataGridView C#

发布于 2025-02-04 21:31:39 字数 417 浏览 2 评论 0原文

我想显示标签一列的所有单元格的 sum 的重新填充。我尝试了一个在网络中看到的代码,但代码不起作用。

这是代码:

foreach (DataGridViewRow row in dgv_Carrinho.Rows.Cast<DataGridViewRow>().Where(t => !string.IsNullOrEmpty(t.Cells["Valor"].Value?.ToString())))
            {
                lbl_Subtotal.Text += (Convert.ToDouble(row.Cells["Valor"].Value)).ToString();
            }

有人可以帮我吗?

I would like to show into a label the resul of the sum of all cells of one column. I tried one code that I saw in the net but the code doesnt work.

Here's the code:

foreach (DataGridViewRow row in dgv_Carrinho.Rows.Cast<DataGridViewRow>().Where(t => !string.IsNullOrEmpty(t.Cells["Valor"].Value?.ToString())))
            {
                lbl_Subtotal.Text += (Convert.ToDouble(row.Cells["Valor"].Value)).ToString();
            }

Could someone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱的那么颓废 2025-02-11 21:31:39

您必须先计算总和,然后将其分配给lbl_subtotal.text,就像

var sum = 0;
foreach (DataGridViewRow row in dgv_Carrinho.Rows.Cast<DataGridViewRow>().Where(t => !string.IsNullOrEmpty(t.Cells["Valor"].Value?.ToString())))
    sum += (Convert.ToDouble(row.Cells["Valor"].Value));

//Now assign it to label
 lbl_Subtotal.Text = sum.ToString();

让我们尝试一些Linq操作一样,

var sum = dgv_Carrinho.Rows.Cast<DataGridViewRow>()
     .Select(double.TryParse(row.Cells["Valor"].Value?.ToString(), out double value) ? value : 0)
     .Sum();
//Now assign it to label
lbl_Subtotal.Text = sum.ToString();

You have to calculate sum first, then assign it to lbl_Subtotal.Text, something like

var sum = 0;
foreach (DataGridViewRow row in dgv_Carrinho.Rows.Cast<DataGridViewRow>().Where(t => !string.IsNullOrEmpty(t.Cells["Valor"].Value?.ToString())))
    sum += (Convert.ToDouble(row.Cells["Valor"].Value));

//Now assign it to label
 lbl_Subtotal.Text = sum.ToString();

Lets try some linq operations,

var sum = dgv_Carrinho.Rows.Cast<DataGridViewRow>()
     .Select(double.TryParse(row.Cells["Valor"].Value?.ToString(), out double value) ? value : 0)
     .Sum();
//Now assign it to label
lbl_Subtotal.Text = sum.ToString();
无声情话 2025-02-11 21:31:39
var count = 0;
foreach (DataGridViewRow row in dgv_Carrinho.Rows.Cast<DataGridViewRow>().Where(t => !string.IsNullOrEmpty(t.Cells["Valor"].Value?.ToString())))
{
     count += (Convert.ToDouble(row.Cells["Valor"].Value)).ToString();
}
lbl_Subtotal.Text = count.ToSring();
var count = 0;
foreach (DataGridViewRow row in dgv_Carrinho.Rows.Cast<DataGridViewRow>().Where(t => !string.IsNullOrEmpty(t.Cells["Valor"].Value?.ToString())))
{
     count += (Convert.ToDouble(row.Cells["Valor"].Value)).ToString();
}
lbl_Subtotal.Text = count.ToSring();
浸婚纱 2025-02-11 21:31:39

您可以为DataGridView使用添加扩展功能

public double getSumCell(this DataGridView GridView, string ColName)
{
    double sumValue = (from d in GridView.Rows
                       select d).Sum(DataGridViewRow x => Convert.ToDouble(x.Cells(ColName).Value));

    return sumValue;
}

lbl_Subtotal.Text = dgv_Carrinho.getSumCell("COLUMN_NAME");

u can add extension function for DataGridView

public double getSumCell(this DataGridView GridView, string ColName)
{
    double sumValue = (from d in GridView.Rows
                       select d).Sum(DataGridViewRow x => Convert.ToDouble(x.Cells(ColName).Value));

    return sumValue;
}

usage:

lbl_Subtotal.Text = dgv_Carrinho.getSumCell("COLUMN_NAME");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文