0
votes

In my form I have a panel with autosize = true.

The panel contains a SplitContainer control with 2 panels arranged horizontally.

The top panel contains a datagridview and the bottom panel contains a textbox.

Is there a way I can programmatically resize the top panel, and hence the entire SplitContainer based on changes in the height of the datagridview (determined by the number of rows)

UPDATE: I am now able to resize the SplitContainer which has Dock = Bottom, however, the Layout event of the parent panel does not get fired in response to changes in the height of the SplitContainer control even thought the parent panel has Autosize = True

2
It would be helpful if you could include how new rows are added!Capn Jack

2 Answers

0
votes

Try setting the splitter distance of the SplitContainer based on the height of the grid.

SplitterDistance Gets or sets the location of the splitter, in pixels, from the left or top edge of the SplitContainer.

Via https://msdn.microsoft.com/en-us/library/system.windows.forms.splitcontainer.splitterdistance(v=vs.110).aspx

Edit What worked for me was to set Dock for the SplitContainer to None, which then allowed me to set the size of the SplitContainer (calling SplitContainer.Height) and having the panel resize itself to fit the SplitContainer.

Edit 2 To allow the SplitContainer to auto-size its width, you can try the following:

splitContainer.Width = this.ClientRectangle.Width - (splitContainer.Location.X * 2)

This.ClientRectangle.Width should get the width of the window without the border (if the parent is the form).

0
votes

For some reason that I dont manage to understand, if I force the panel size in the code the object gets the right size. If I define the panel size 300x300 in the IDE, later is shown smaller and the lines are not visible.

 public Form1()
    {
        InitializeComponent();
        panel1.Size = new Size(300, 300);
    }