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 :| )
I'd like to automatically resize my datagridview's width to fill the width of the form. After simply placing the datagridview on the form, the grid stays the same size when the form is resized. That happens even when I use the little constant-distance-snap thing 'connecting' the control to the form's borders.
Do I have to handle a form.resize event of some sorts, or is there a property I can set in VS?
This question is related to
c#
winforms
datagridview
resize
You have to chose 'Fill' in the Dock property.
set the "Dock" property of datagridview in layoutto one of these properties : top, left, bottom, right. ok?
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.
Set the property of your DataGridView:
Anchor: Top,Left
AutoSizeColumn: Fill
Dock: Fill
In your form constructor you could create an event handler like this:
this.SizeChanged(frm_sizeChanged);
Then create an event handler that resizes the grid appropriately, example:
private void frm_sizeChanged(object sender, EventArgs e)
{
dataGrid.Size = new Size(100, 200);
}
Replacing those numbers with whatever you'd like.
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.
Set the anchor property of the control to hook to all sides of the parent - top, bottom, left, and right.
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
Unless I am misunderstanding what you are asking you can do this on the properties for your data grid view. You need to set the Anchor property to the sides you want it locked to.
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.
For me, anchoring works only if I set it to all four sides:
Anchoring: Top, Bottom, Left, Right
Setting anchoring just to Left, Bottom moves the whole object when the form is resized in bottom, left side. Setting all four sizes really resizes the object, when parent is resized.
Source: Stackoverflow.com