Click here to Skip to main content
11,928,605 members (53,053 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# Windows Forms
I have a C# Windows Forms Application designed with MS Visual Studio 2010 Ultimate. Everything works fine except I am unable to get the DataGridView which makes up most of the space on the form to Maximize when the user chooses to maximize the form to fill up the whole screen.

I there a property or some other coding technique I can apply to get the DataGridView to expand with the Form?

Thank you.
Posted 25-Apr-13 12:08pm
joshrduncan2012 25-Apr-13 17:17pm
Anchor property?
Sergey Alexandrovich Kryukov 25-Apr-13 18:00pm
This is possible, but in most cases, this way is clearly inferior compared to Dock combined with Padding. I explained it in my answer, please see.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You need to use the property Dock of your control, with the value other then System.Windows.Forms.DockStyle.None (which is the default):[^],[^].

In this case, the control will be resized automatically.

One comment to the suggestion of joshrduncan2012 to use Anchor: they are often useful, but in most cases, prefer docking approach. Some controls tend to flicker during resize operation if anchored. Also, it's more of a manual work or coding: you would need to take care about position and size at the same time, for symmetry, and bad for maintenance. With docking approach, this is not a problem: use it with Padding:[^],[^].

Espen Harlinn 25-Apr-13 18:27pm
Sergey Alexandrovich Kryukov 25-Apr-13 18:44pm
Thank you, Espen.
stevenandler 26-Apr-13 10:57am
I set the Dock to Fill. The only problem I have now is there are some labels and text boxes at the top of the form and the datagridview is now superimposed on these items. Can you tell me what other property I can set which will protect the top part of the form from being covered by ther datgridview?
Sergey Alexandrovich Kryukov 26-Apr-13 11:09am
You just screw up something, it happens. Here is the idea: don't put anything in the form, except some panel. Dock absolutely everything, and the "real" controls should be placed in some panel, one or another in a hierarchy of docked panels padding them. Sparingly, use the Splitter, but only when it makes sense. It takes little experience, but very soon you will clearly see how to do it. I personally often use the Designer to put main menu, status bars and those panels, and add everything else in code. For some complex layout, it's good to colorize the panels, to clearly see how things are moving on resize; later you can remove these colors... Just a little designer's secret...
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Thank you for your help. It took me a little while to fugure this out and I did need to provide some padding in order for the grid headers not to be chopped off.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151126.1 | Last Updated 3 May 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100