如何在GridView中减去两列值 [英] how to minus two columns value in GridView

查看:74
本文介绍了如何在GridView中减去两列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何减去列成本,保险成本并将结果存储在每行的总计?!



myCode:

how to minus column Cost,insuranceCost and store the result in a column Total for each row?!

myCode:

ServiceInsuranceList = new System.Data.DataTable();




private void BtnAdd_Click(object sender, EventArgs e)
       {

             GrdServiceInsurance.DataSource = ServiceInsuranceList;
                for (int i = 0; i < GrdServiceInsurance.Rows.Count; ++i)
                {
 GrdServiceInsurance.Rows[i].Cells["Total"].Value =(Convert.ToInt32( GrdServiceInsurance.Rows[i].Cells["Cost"].Value.ToString())) -(Convert.ToInt32( GrdServiceInsurance.Rows[i].Cells["InsuranceCost"].Value.ToString())) 
                }

       }





但它不起作用......

错误对象引用不是设置为对象的实例。



But it does not work...
error "Object reference not set to an instance of an object."

推荐答案

首先,您必须检查成本和保险成本是否为空或空白。如果其中一个为空或空白则出现错误。所以null或空白从DB端转换为0.
first you have to check whether cost and insurance cost is not null or blank. if one of the null or blank then error come. so null or blank convert into 0 from DB side.


检查一下,其中一半与你的相似:显示GridView的页脚总数,并在最后一列中添加列的总和(行虎钳) [ ^ ]
Check this out, half of it is similar to yours: Displaying Total in Footer of GridView and also Add Sum of columns(row vise) in last Column [^]


我建​​议您在数据表本身中再添加一列Total,并使用数据表迭代而不是gridview将快速解决您的问题。



在上面的代码中缺少GrdServiceInsurance.databind()。
i will suggest you to add one more column Total in your datatable itself and assign value using datatable iteration instead of gridview that will be fast & solve your problem.

In the above code GrdServiceInsurance.databind() is missing.


这篇关于如何在GridView中减去两列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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