50

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?

13 Answers 13

66

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.

2
  • fyi, if you don't anchor the bottom as well then the grid can grow large enough to slide behind any bottom docked objects in the same container.
    – TWood
    Mar 10, 2014 at 17:49
  • I should also note that if you have a MaximumSize or MinimumSize Properties set to anything other than default (0, 0), none of this will have any effect.
    – Ben
    Mar 13, 2015 at 3:06
30

Set the property of your DataGridView:

Anchor: Top,Left
AutoSizeColumn: Fill
Dock: Fill
1
  • 7
    It is best to also give at least a short explanation of how it works so that folks reading won't have to mentally parse it line by line to understand the differences.
    – Fluffeh
    Sep 28, 2012 at 10:44
12

Set the anchor property of the control to hook to all sides of the parent - top, bottom, left, and right.

1
  • 1
    I'm using VS2015, to get the DataGridView to fill the screen, I had to use the above settings + AutoSizeColumnsMode = Fill...Tried default Anchor setting = top,left + AutoSizeColumnsMode = Fill + AutoSizeRowsMode = Fill and other options, but did not work.
    – Chris
    Nov 30, 2017 at 17:56
7

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;
6

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.

5

You have two options here:

  • Option one, Anchor
  • Option two, Dock

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

3

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.

1
  • That would work, but it's more time intensive that simply using the tools the framework makes available. Thanks for the name of the event tough, should I ever need to fall back on a custom resize event handler after all.
    – lowerkey
    Nov 18, 2011 at 14:02
1

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.

1

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.

1

You have to chose 'Fill' in the Dock property.

0

set the "Dock" property of datagridview in layoutto one of these properties : top, left, bottom, right. ok?

1
0

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.

0

The docs on Anchor & Dock noted by Daniel Mošmondor states that Anchor & Dock are mutually exclusive.

I set the Form layout property:

AutoSizeMode to GrowAndSrink

Then set the DataGridView layout properties:

Anchor to Top, Bottom, Left, Right

(by clicking the down arrow-head then clicking on the 2 horizontal and 2 vertical bars)

This resized the DGV with the Form, but the top of the DGV was behind the buttons at the top of the Form.

So I set the DGV layout property:

Marign Top to 40 to clear the bottom of the buttons.

This worked.

Adjusting the Margins for Left, Right and Bottom on the DGV did not seem to make a difference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.