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?
In the user control I have placed a textbox and a button. On the click event of the Button I am calling the Parent Page method we discussed above using Reflection and passing the value of the textbox to the method and then the method is invoked and the message is displayed on the screen. It works fine for me.
C# user control is defined as an implementation in programming language of C# to provide an empty control and this control can be leveraged to create other controls. This implementation provides additional flexibility to re-use controls in a large-scale web project.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With