Click here to Skip to main content
Rate this: bad
good
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
Comments
joshrduncan2012 at 25-Apr-13 17:17pm
   
Anchor property?
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
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):
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
  Permalink  
Comments
Espen Harlinn at 25-Apr-13 18:27pm
   
5'ed!
Sergey Alexandrovich Kryukov at 25-Apr-13 18:44pm
   
Thank you, Espen.
—SA
stevenandler at 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 at 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
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 BillWoodruff 360
1 Mathew Soji 309
2 DamithSL 225
3 Afzaal Ahmad Zeeshan 202
4 Maciej Los 190
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 3 May 2013
Copyright © CodeProject, 1999-2014
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