开发者

How to call a method in a UserControl after it is shown?

I have a multi-paned form, in the left pane is a tree and in the right pane is a Panel. Tree selections result in specific UserControls being loaded on the Panel. In this case, since the parent form is always loaded, I cannot hook into the parent form's Shown event.

Most of my UC's have an unbound DataGridView on them which needs to be populated when the UC is created. There can be 50,000 rows added up front at times, from an SQL Server database.

Up until now I 开发者_运维百科have populated the grids during the UC constructor, but since we started working with 50,000 rows things have changed. I have found that populating the grid with 50,000 rows from the constructor takes at least 15 minutes for some crazy reason. However, if I wait to populate the grid until I click a button or from the Load event, it takes 15 seconds. This is a mystery. So I am trying to move the loading of the grid elsewhere.

When I try populating the grid from the Load event, however, there are visual inconsistencies. What happens, is you see a small version of my UC appear, it takes 15 seconds to load the DGV rows, and THEN the UC expands to fill the Panel (the UC Dock property is set to Fill). So I don't like this option either.

The Shown event would be a perfect place to load my grid, if it existed for UC's. Does anyone know of another way to do this?


My answer to a similar problem was to populate the DGV when the grid first became visible. This is the essence of lazy-loading; get the information at the last possible second.

Hook into VisibleChanged, which will fire when your control has Show() or Hide() called, or the Visible property manually set. If the field is currently visible, AND the control is not in the process of closing or being disposed (because for some freaky reason controls can become visible in this case), perform your grid population.

Here's the necessary handler, very simple:

protected override void OnVisibleChanged(EventArgs e)
{
    base.OnVisibleChanged(e);

    if (Visible && !Disposing) PopulateGridView(); //<-- your population logic
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜