Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 24-Dec-17 4:16am
Comments
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.
—SA

You need to use the property Dock of your control, with the value other then System.Windows.Forms.DockStyle.None (which is the default):
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dock.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.dockstyle.aspx[^].

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:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.padding.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.padding.aspx[^].

—SA
 
Share this answer
 
Comments
Espen Harlinn 25-Apr-13 18:27pm    
5'ed!
Sergey Alexandrovich Kryukov 25-Apr-13 18:44pm    
Thank you, Espen.
—SA
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...
—SA
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900