I have an ultragrid that I am embedding within another control, but I am having some difficulty in getting the ultragrid to render properly. I believe this has to do with some timing issues in the following sense: When I embed the ultragrid in my custom control, I am taking an image/snapshot of the control at a particular moment and then inserting that image into my control (it's a report control, basically). What I am seeing in the report looks like the attached image...it seems that the visible rows haven't been populated yet. I've confirmed in my code that the appropriate HandleCellDataRequested events have fired (basically the datasource has been pinged, and all of the data has come into the grid). It just appears that the data is not showing for some reason.
To elaborate on this a little further, I should point out that I put the *exact* same grid code into a plain old form (one that doesn't take an image snapshot or anything) and rendered the ultragrid with data just fine (as expected); however the <UltraGrid>.Rows.VisibleRowCount (and ActiveRowScrollRegion.VisibleRows.Count) are 0 for a while after the creation of the form. It takes a few computation cycles for the Form to fully load...and *only then after the form seems to be fully loaded* does the VisibleRows property populate appropriately. The question I'm leading to is the following:
- Does the ultragrid listen for any parent control load events before creating the grid rows? If so, I'd love to know which this is so that perhaps I may mock this event in order to force the row creation (since it's possible for my scenario with the image/snapshot, the grid doesn't seem to be loading the rows in time)
- Does that ultragrid in the picture (with the headers appearing slightly grey) tell you anything about the state of the grid? I.e. does it look like it was in a "loading" state?
Thanks!
Hi,
How are you taking the snapshot of the grid?
My guess is that the grid isn't creating the row because it has never painted itself on the screen. So you probably just have to call grid.Update to force a paint.
I like what you're getting at with regards to the painting, but it appears grid.Update() didn't do the trick for me. Does the ultragrid construct the headers even before the official painting of the rows occurs? (Per the attached picture from my original post, only the headers are here)
The way that the snapshot is being taken is through a User32 Send message WM_PRINT = 0x0317. Specifically,
SendMessage(<control Handle>, msgWM_PRINT, <Call GetHDC from the graphics object>, <Additional draw options here>);
tamalecharley said:Does the ultragrid construct the headers even before the official painting of the rows occurs?
It might. I can't be completely sure without seeing it in action. The only explanation for this behavior is that you are trying to take an image of the grid before the rows have been created. So it's a matter of either changing the timing so that you wait until after the rows are generated, or you force the row to get created before you take the snapshot.
I'm surprising Update didn't work. That must mean that the grid is not prepared to create the rows even if it does paint. So that could mean that the grid has no BindingContext (hasn't been parented to the form, yet). You could try setting the grid's BindingContext to the Form's BindingContext (or even a new BindingContext) and then calling Update to see if that helps.
BTW... you might also want to try using the DrawToBitmap method instead of using the SendMessage Windows API. It's a lot easier and it's managed code, and it might cause the control to paint.
No, I don't think that's a problem, unless thing BindingContext hasn't been set at all, yet.
It doesn't really matter what the context is - it just has to have a context (any context) in order to get data. The context is used mainly to keep various bound control in the same container in synch in terms of position.
Something that I failed to mention: When digging into this problem in the early stages, I was noticing that the BindingContext wasn't being filled into the grid when it was being created. Previously we didn't need to worry about this because I think this gets created at the time of Controls.Add(...) or something...In our case we are assigning the grid to a container via a dynamic property (which then goes and adds it to the control list). That being said, for some reason the BindingContext wasn't being created, so I just created it myself with this.BindingContext = new System.Windows.Forms.BindingContext();
I saw in a separate post on the forum that this had solved someone's problem with attaching a datasource that wasn't working, so I figured it would do the trick for me as well. Do you think this could be related to the issue here? Is there a different BindingContext I should be using?
It sounds to me like the parent-control relationship might not be set yet, but I'm not sure how to achieve this if it's not already happening (form.Controls.Add(..) is happening at some point as I just mentioned)