如何在gridview结尾处对gridview的值求和并显示结果 [英] How to Sum the value of gridview and show result at the end of that gridview

查看:98
本文介绍了如何在gridview结尾处对gridview的值求和并显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii ...



我正在使用Windows应用程序而且我遇到了问题。假设有一个gridview,我必须在其中添加特定列的值。并且在行的末尾它将显示结果。就像,



无。 项目 费率



1铅笔10

2方框20



总计:30





我写了这段代码,

Hii...

I am working on Windows Application and I am stuck in an issue. Suppose have a gridview in which I have to add the value of a particular column.And at the end of the rows it will display the result.Like,

No. Item Rate

1 Pencil 10
2 Box 20

Total: 30


I have written this code,

private void datagridview_CellValueChanged(object sender, DataGridViewCellEventArgs e)
       {
           if (e.ColumnIndex == 2)
           {
               decimal Total = 0;
               for (int i = 0; i < datagridview.Rows.Count; i++)
               {
                   if (Convert.ToString(datagridview.Rows[i].Cells[2].Value) != "")
                   {
                       decimal GrossAmnt = Convert.ToDecimal(datagridview.Rows[i].Cells[2].Value.ToString());
                       Total = GrossAmnt + Total;
                       datagridview[2, datagridview.Rows.Count].Value = Total.ToString();
                       
                   }
               }


           }
       }





执行此操作时显示错误;



when I execute this it shows me an error;

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index







有人在那里请帮我解决这个问题。





谢谢...




Anybody is there please help me to solve this.


Thanks...

推荐答案

int count = 0;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                count = count+ Convert.ToInt32(ds.Tables[0].Rows[i][2].ToString());
            }
            DataRow dr = ds.Tables[0].NewRow();
            dr[0]="";
            dr[1] = "Total::";
            dr[2]=count.ToString();
            ds.Tables[0].Rows.Add(dr);


这篇关于如何在gridview结尾处对gridview的值求和并显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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