I found a lot of questions about how to resize the form when a child control resizes, but I\'m trying to do something much simpler (maybe so simple people don\'t even ask it
The 'Anchor' property exists for any container: form, panel, group box, etc.
You can choose 1 side, left for example, or up to all four sides.
Anchor means the distance between the side(s) chosen and the edge of the container will stay the same, even upon resizing.
E.g., A datagridview, dgv1
, is in the middle of Form1
. Your 'Anchor' the left and top sides of dgv1. When the app is run and resizing occurs, either from different screen resolutions or changing the form size, the top and left sides of dgv1
will change accordingly to maintain their distance from the edge of From1
. The bottom and right sides will not.
If you want to show the complete headers text
this will auto resize the columns so that the headers will show complete header text.
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
For Dock Mode
If you want to show the Dock Mode in your panel or form.
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
If anyone else is stuck with this, here's what helped me. Changing the Anchor settings did not work for me. I am using datagridviews within groupboxes in a form which is inside a parent form.
Handling the form resize event was the only thing that worked for me.
private void Form1_Resize(object sender, EventArgs e)
{
groupBoxSampleQueue.MinimumSize = new Size((this as OperatingForm).Width - 22, 167);
groupBoxMachineStatus.MinimumSize = new Size((this as OperatingForm).Width - 22, 167);
}
I added some raw numbers as buffers.
You have two options here:
Look for both properties and figure out which one suit your needs.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.anchor.aspx
and
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dock.aspx
Use control anchoring. Set property Anchor
of your GridView
to Top, Left, Right
and it will resize with container. If your GridView
are placed inside of some container (ex Panel
) then Panel
should be anchored too.
set the "Dock" property of datagridview in layoutto one of these properties : top, left, bottom, right. ok?