如何获取gridview行值的总和到标签? [英] How to get sum of the gridview row values to the label?

查看:101
本文介绍了如何获取gridview行值的总和到标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview ...它包含3列日期,名称和总数...我需要计算总数,并且它将显示在标签上...如果总数为10、20、30,则标签应该显示60 ...请帮助

hi i have a gridview...it contain 3 column date, name and total...i need to calculate the sum of the total and it will display on label...if total is 10,20,30 then label should display 60...please help

推荐答案

只需遍历DataGridViewRows循环即可.

Just iterate through the loop of DataGridViewRows

private void button1_Click(object sender, EventArgs e)
       {
           decimal total = 0;

           foreach (DataGridViewRow row in dataGridView1.Rows)
           {
               if (!row.IsNewRow && row.Cells["Total"].Value != null)
               {
                   total += (decimal)row.Cells["Total"].Value;
               }
           }
             
            label1.Text = total.ToString("N2");
       }


int Cnt;
For(int i=0;i<gridview1.rows.count-1;i++)>
{
Cnt+=Convert.ToDouble(Gridview1.Rows[i].Cells[2].Text);
}
lblTotal.Text=Cnt.toString();




如果要通过StoredProcedure获取数据,则只需在返回表中再添加一行,并包含所有值的总和.而且这对您来说更容易计算.甚至您可以返回多个表,因此第二个表将包含总计.

希望它对您有用
谢谢.
Hi,

If you are fetching data through the StoredProcedure then you can simply add one more row in the return table with total of all the values. And this would be easier for you to calculate. or even you can return multiple tables so the 2nd table will contain summation of your total.

Hope it works fine for you
Thanks.


这篇关于如何获取gridview行值的总和到标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆