I have the result of a linq query that's binded to a grid开发者_开发知识库view. OnRowDataBound I'm creating an event handler that's looping through the columns and making changes to the data.
Is it better to loop through the columns of the gridview or through the datasource?
OnRowDataBound
you're getting the data as it's being bound, so you only need to focus on the row in question, not the whole grid.
You should be working with the gridview control at this point, not the data source, but the datasource of the row is available in the second passed variable so you can access it there (I call mine e
) if you need the actual source values rather than what got bound. But by the time the row is presented to you in this event, it's already got the data.
However, it's almost always going to be faster to do all the data manipulations together in a tight loop and let the jitter add any microops the CLR team can provide, rather than trying to do it as a sparse loop.
I would say it is up to you. But here some details:
If you want to apply any conditions(filter) which probably reduced the amount of data, in such case better do it before Databind()
. But if according with your conditions/logic amount of data staying the same it is doesn't mater where you looping it. It still will be the same loop, same performance.
If you want to check performance use class Stopwatch
from System.Diagnostics
.
精彩评论