简体   繁体   中英

Forcing DataGridView to fill available space in SplitContainer

I'm in the process of writing a Windows Forms application that uses two nested SplitContainers with DataGridViews in two out of the three available areas, as shown in the following image. I've wired the Settings and form initialization to allow the user to disable the multi-pane formats in favor of a single-pane view consisting of just the first DataGridView.

The primary issue here (as I will explain in (hopefully) enough depth), is that the DataGridViews do not adhere to the constraints of the SplitContainer panels if the other panels are hidden.

DataGridView / SplitContainer布局

Here, the red outline indicates the parent SplitContainer, called masterSplitContainer appropriately in the references. The blue outline indicates the child SplitContainer, titled bottomSplitContainer , occupying the masterSplitContainer.Panel2 position.

DataGridViews occupy both the masterSplitContainer.Panel1 and bottomSplitContainer.Panel1 areas, and I've designed the initialization code so far so that the program accounts for three possibilities:

  1. The user wants a single-pane only view/UI layout, thus making masterSplitContainer.Panel1 occupy the entire area of masterSplitContainer .
  2. The user enables the dual-pane layout, but removes Panel 2 of the bottom container .
  3. The user wants all three panels.

As my test builds with only the SplitContainers seemed to indicate, the second SplitContainer (the child) got the intended point and was perfectly happy with this arrangement, with no snags whatsoever. However, when the settings to enable single-pane views are tested, here is the result:

DataGridView问题1

The masterSplitContainer.Panel2 is disabled with the following code:

masterSplitContainer.Panel2Collapsed = true;
masterSplitContainer.Panel2.Hide();

In this case, I would like the DataGridView to assume the full operating area of the masterSplitContainer, rather than retaining its predefined boundaries, which were set simply because that was where the panel splitter was when I added the part.

For process of elimination, the same thing occurs with the second DataGridView placed in the child SplitContainer (that is, bottomSplitContainer.Panel1 ) - it also does not obey the area of its enclosure.

I've done a little bit of reading on this so far and it looks like this may be an issue of anchoring, but I have very limited experience with .NET and Windows Forms so this may take a little bit of hand-holding or pointing out obvious mistakes.

Also, if y'all need more actual code references, I'm happy to post the form class upon request.

Try this:

Add the following Method:

public static void DoubleBuffered(this DataGridView dgv, bool setting)
    {
        Type dgvType = dgv.GetType();
        PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",
            BindingFlags.Instance | BindingFlags.NonPublic);
        if (pi != null) pi.SetValue(dgv, setting, null);
    }

Call it like that:

dataGridView1.DoubleBuffered(true);

dataGridView1 has be the one in your splitpanel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM