且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

绑定数据时,如何使2列的总和在gridview中的第3列中显示

更新时间:2023-12-01 09:43:52

您可以遍历数据源以查找所需的数据,然后,您可以在数据源中添加一列,然后用您需要的数据填充它.假设数据来自数据库,那么进行计算并将该列添加到数据库中就更有意义了.
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