绑定数据时,如何使2列的总和在gridview中的第3列中显示 [英] How to make sum of 2 columns and show in 3rd column in gridview while data is binding

查看:209
本文介绍了绑定数据时,如何使2列的总和在gridview中的第3列中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据表和网格视图.我已将数据源属性分配给网格视图

现在我想计算两列的总和,并在另一列或标签中显示该如何显示??????

在这种情况下,我可以获取这些列的值. ????

在此先谢谢您

Hi i have a data table and grid view. i have assigned data source property to grid view

now i want to calculate sum of the two columns and show it in another column or in label how do i show that????????

in which event i can get these columns values. ????

Thanks in advance

推荐答案

您可以遍历数据源以查找所需的数据,然后,您可以在数据源中添加一列,然后用您需要的数据填充它.假设数据来自数据库,那么进行计算并将该列添加到数据库中就更有意义了.
You can iterate over your data source to find the data you want, and as you do that, you can add a column to your data source and fill it with the data you need. Assuming where the data comes from is a database, it would make more sense to do your calculation and add the column in the DB.


如果要在UI级别或BL级别进行操作,您可以创建一个新的数据表作为数据源.为此,请定义查询结果中的预期列,并添加一个新列作为所需的总和.

从DB获得的数据表逐行迭代.逐行设置所有列的值.可以这样说:3
If you want to do at UI level or BL level, you can create a new datatable as your datasource. For that, define the expected columns from query result and add a new column as sum that you need.

Iterate Row-wise though the datatable you got from DB. Set the values of all the columns row-wise. Lets say, something like:3
foreach (DataRow dr in dt.Rows)
        {
            tempValueIn1stColumn = Convert.ToDouble(dr["1st Column Name"]);
            tempValueIn2ndColumn = Convert.ToDouble(dr["2nd Column Name"]);
            tempValueIn3rdColumn = tempValueIn1stColumn + tempValueIn2ndColumn; 
            // Set the three values in the new table now. 
        }



尽管可以这样做,但是为了获得最佳性能,建议在数据库中这样做.



Though this can be done, but for best performance doing this in database would be advisable.


在GridView中,您可以使用rowdatabound事件,在该事件中您可以找到第三列标签控件并进行设置您身边的文字.

从你身边检查.

谢谢.

Mehul Thakkar
In GridView, you can use rowdatabound event, in which you can find the third column label control and set the text from your side.

Check from your side.

Thanks.

Mehul Thakkar


这篇关于绑定数据时,如何使2列的总和在gridview中的第3列中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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