Hi!
I have a query on firing of ultragrid AfterRowActivate event:
Description:
I have a Infragistics tree control where when you click on any of the nodes, we query the DB and load the adjacent lying ultrawingrid with the db returned datatable.
so, in the Tree_Select event, i have the code where we assign the data source to the ultrawingrid.
In the normal flow, in tree select event - we assign the data source and then after executing some code logic, it reaches onPaint() event of the grid which ultimately triggers the grid'd afterrowactivate event.
In a special case, where the user is trying to first click(mouse down) on any one of the tree node and without releasing the mouse button, immediately, drags the pointer to some other node in the tree (say the next node) and releases the mouse button there, the flow goes like this:
On mouse down of the first node n1, it triggers the Tree_Select event where we assign the appropriate data source to the grid but before it could reach the AfterRowActivate event of the grid, it again hits the Tree_Select event , this time for the node on which the mouse was released (say n2).
Now, in this event, as soon as it executes the code where we assign the datasource for the grid, it triggers the AfterRowActivate event of the grid(it doesnot go thru the normal process of going thru the next set of stmts. and OnPaint() event to trigger the AfterRowActivate event). This is causing some undesired behavior. Upon closer look, found that it actually triggers the AfterRowActivate event with the data for node n1, executes it and then again hits it with the data for the desired node n2.
I want to prevent the first AfterRowActivate event from getting fired but am not able to achieve that.
My findings:
- I tried to first set the data source as null and then assign the datasource from DB returned DataTable but got same behavior.
Please help to achieve this. From browsing through the forum i came to know that whenever we change the activerow, AfterRowActivate should fire. So, it makes sense to me that it gets fired upon OnPaint() event, but how can i prevent it for the above mentioned case where we are assigning the data source.
Awaiting your valuable suggestions,
Thanks,
Ron
Hi Ron,
Is there any chance you can create a small sample project to demonstrate this behavior?
There's obviously no way that the grid can know that the DataSource is going to get changed twice and therefore ignore the first one.
But something must be forcing the grid to paint while the first datasource is assigned and you then assign the second one and therefore fire the event. Either that or something else is forcing the event to fire without a paint.
Perhaps one way you could get around this is to use a timer to set the DataSource on the grid asynchronously. Basically, instead of using the tree event to directly set the DataSource on the grid, all the tree does is start a timer with a small interval - say 1 second. Then you trap the timer tick and set the DataSource in that event. This way, when the second node is selected, the timer will be re-started before the Tick event has fired. Thus, you will never set the grid's DataSource to the first data source, only the second one.
Thanks Mike for taking the pain of going through my long post and your valuable suggestion on this.
Actually, before posting this question, i tried to simulate the problem in a small project so that i can isolate the issue easily but could not make the "assigning of datasource " to trigger AfterRowActivate Event.
Today, i tried the following thing and kind of worked for me. Let me know your thoughts on this - if this is correct or a good solution:
Basically, in the AfterRowActivate method, i am actually appending some columns to the Datatable i used as a datasource in the Tree_AfterSelect method.But, since in the above mentioned case, in this event: ActiveRow points to node n1 but the datatable has already got the data for n2, a mismatch occurs and "key not found" error is thrown.
As a solution, i tried ,In the beginning of the Tree_AfterSelect event, i make the datasource of the grid = null.
(Note that still i have not made the DB Query to update my datatable which i am gonna use for assigning datasource to the grid.)
On executing the datasource=null stmt, it still is hitting the AfterRowActivate event but the datatable,at this stage, still has the node n1's data. So, adding additonal columns doesnt throw any errors but again it gets back executes the code where i first update the data table with n2 and then assign it to the grid.Now, the active row as well as the datatable both will have n2 in context and execute properly.
I am pretty excited to know your thoughts on this solution or i should try some other approach.
I'm afraid I'm really not sure what's happening there, so it's impossible for to guess whether the solution is a good one.
I don't think adding columns of data to the DataSource in the AfterRowActivate event of the grid is a good idea, though. Why not add the columns to the data source before you assign it to the grid?
In the afterRowActivate event, you can use a boolean set to false to bypass its initial pass or recursions similar to below:
'Global Variable
Dim stopRowActivate As Boolean = False
If stopRowActivate <> True Then stopRowActivate = True ugdTest.ActiveRow = ugdTest.Rows(0) stopRowActivate = FalseEnd If
Hope this helps anyone with this question.