lowerkey
lowerkey

Reputation: 8345

How to resize datagridview control when form resizes

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?

Upvotes: 51

Views: 161844

Answers (13)

Ian Cowell
Ian Cowell

Reputation: 1

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.

Upvotes: 0

Conair
Conair

Reputation: 21

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.

Upvotes: 0

Muhammad Muneeb
Muhammad Muneeb

Reputation: 71

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;

Upvotes: 7

OldProgrammer
OldProgrammer

Reputation: 116

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.

Upvotes: 6

Jule
Jule

Reputation: 11

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

Upvotes: 1

FrenkyB
FrenkyB

Reputation: 7207

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.

Upvotes: 1

user2821937
user2821937

Reputation: 119

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

Upvotes: 0

wreckseal
wreckseal

Reputation: 321

Set the property of your DataGridView:

Anchor: Top,Left
AutoSizeColumn: Fill
Dock: Fill

Upvotes: 32

brpyne
brpyne

Reputation: 1006

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.

Upvotes: 4

Daniel Mošmondor
Daniel Mošmondor

Reputation: 19986

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

Upvotes: 5

Taryn
Taryn

Reputation: 247860

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.

Upvotes: 1

hmqcnoesy
hmqcnoesy

Reputation: 4224

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

Upvotes: 12

Samich
Samich

Reputation: 30175

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.

Upvotes: 66

Related Questions