从datagridview轻松添加 [英] Simple Adding from datagridview

查看:78
本文介绍了从datagridview轻松添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个应用程序,需要计算数据网格中列的所有值的总和,只是为了向用户显示选择时花在工作代码上的金额.
我调用了一个数据集并查看了所有数据项并尝试添加它,但是异常不断出现,我尝试了所有我知道的简单但不起作用的方法

我要添加的字段是数据库中的长整数,我使用数据集填充表
然后我调用了add函数.

任何人都可以帮忙.不要因为我是初学者而简单地伤害我的感情

我的代码

I am working with an application and need to calculate the sum off all values of a column in data grid, just to show the user how much amount he spent on the job code when he selects it.
I called a data set and view all the data items and tried to add it but exception keeps on coming I tried all the ways I know its simple but not working

My field to be added is Long integer in database and I fill the table using dataset
and after that I called the add function.

Please can anyone help. Don''t avoid hurting my feelings simple because I am a beginner

My code

public DriverPaymentForm(DriverPaymentBean driverpaymentbean)
        {
            InitializeComponent();

            fillForm(driverpaymentbean);
            showallPaymentbyjobcode();
            addtotalpaid();

        }

public void showallPaymentbyjobcode()
        {
            try
            {



                String query = "Select jobcode ,vehicleno,amount,postdate,description,ischeque,iscash from driverpaymentmastertable  where jobcode Like '" + txtjobCode.Text + "'  Order by postdate";
                OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
                DataSet ds = new DataSet();
                dAdapter.Fill(ds, "tblpayview");
                tblpaymentview.DataSource = ds.Tables["tblpayview"].DefaultView;
                if (ds.Tables.Count <= 0)
                {
                    lblstatus.Text = "No Payment Details Present";
                }
                
            }
            catch (Exception)
            {
                MessageBox.Show("The application had met with some errors please restart  the application :\n error:closer MSAccess files");

            }


        }

        public void addtotalpaid()
        {
           long  sum = 0;
            int count = tblpaymentview.RowCount;
            if (count != null&count!=0)
            {

                for (int i = 0; i < count; i++)
                {

                    sum = sum + long.Parse(tblpaymentview.Rows[i].Cells[3].Value.ToString());

                }
            }

        }



附加行上的异常null引用异常



Exception null reference exception at addition line

推荐答案

如果这是您的实际代码,请替换
If this is your actual code start by replacing
if (count != null&count!=0)

if (count!=0)



接下来添加检查以查看单元格值是否不为空



Next add a check to see if the cell value is not null

if( tblpaymentview.Rows[i].Cells[3].Value != null )
{
  sum = sum + long.Parse(tblpaymentview.Rows[i].Cells[3].Value.ToString());
}



由于您的代码使用了付款一词,因此您可能希望使用double而不是long,因为货币金额通常使用数字.



As your code uses the term payment you may want to use double instead of long as monetary amounts usually have digits.


这篇关于从datagridview轻松添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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