Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello my program is a vb.net windows forms application and I'm looking for a simple way to autosize my datagridview to get rid of the blank gray background areas. I know that an easy way to do this is to change the background color to match the form, however I'm looking to resize the datagridview to never show the blank gray area. Is there a simple way to do this? I greatly appreciate any help or suggestions you may offer. Thanks!
Posted
Comments
Sergey Alexandrovich Kryukov 16-Oct-13 15:49pm    
Not so easy, as far as I can remember; I also had problems with that. It would be much better not to control the grid view size. What happens if it is docked in a resizeable panel? (And I strongly believe everything should always be docked.)

Rather, you should make the last column resize to occupy all free space remaining on right, it it is available. I noticed that all well-established Microsoft applications (especially those bundled with the OS) do that perfectly, but many 3rd-part applications suffer from the problem you described. So, maybe Microsoft is hiding something undocumented, again? Or we just did not solve the problem properly? Anyway, I'll up-vote this question with 4 (not 5 just because resizing the whole control is a wrong idea). If I have a chance to get to it and even solve it (I cannot promise), I'll let you know...

—SA
CAS1224 16-Oct-13 15:57pm    
Great! Thanks for the information

1 solution

The DGV doesn't have the AutoSize property and doesn't support resizing itself to its content.

The only way I can think of to do this would be to enumerate the colums, get their widths and add them up, then set the width of the DGV to that, plus a little extra probably for a scrollbar and borders.
 
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