Click here to Skip to main content
Licence 
First Posted 10 Apr 2005
Views 65,040
Bookmarked 47 times

Autoresize a DataViewGrid to never have horizontal scrollbar

By | 10 Apr 2005 | Article
When you resize a form you don't want to have a horizontal scrollbar on a DBGrid. This code snippet resizes all the columns in a grid in an intelligent way.

Introduction

The article describes how you can resize a DBGrid to fit the form after a resize, so that there is no horizontal scrollbar. To use it, you need the following code in the form where your DBGrid is sitting:

   int PrevWidth;
   public Form1()
   {
       InitializeComponent();
       PrevWidth = dataGrid.Width;
   }

   private void Form1_ResizeEnd(object sender, EventArgs e)
   {
       dataGrid_Resize(dataGrid, ref PrevWidth);
   }

You can also call it from the Resize event. The advantage is that it does a nice resizing when the user resizes, but the disadvantage of it is that a lot of silly CPU and columns are calculated many a times, and that it will be messed up if the form is slowly sized. So it is better to deal with the ResizeEnd event. This is the code that does the horse work. I put it in a static class so that all the forms can use the same code to do the resizing (that's why the PrevWidth is held in the form itself).

       public void dataGrid_Resize(object sender, ref int PrevWidth)
       {
          DataGridView dataGrid = (DataGridView)sender;

          if (PrevWidth == 0)
              PrevWidth = dataGrid.Width;
          if (PrevWidth == dataGrid.Width)
              return;

          //const int ScrollBarWidth = 18;
          //int FixedWidth = ScrollBarWidth + dataGrid.RowHeadersWidth;

          int FixedWidth = SystemInformation.VerticalScrollBarWidth + 
                                           dataGrid.RowHeadersWidth + 2;
          int Mul = 
             100 * (dataGrid.Width - FixedWidth) / (PrevWidth - FixedWidth);
          int W;
          int total = 0;
          int LastCol = 0;

          for (int i = 0; i < dataGrid.ColumnCount; i++)
              if (dataGrid.Columns[i].Visible) {
                  W = (dataGrid.Columns[i].Width * Mul + 50) / 100;
                  dataGrid.Columns[i].Width = 
                              Math.Max(W, dataGrid.Columns[i].MinimumWidth);
                  total += dataGrid.Columns[i].Width;
                  LastCol = i;
              }
          total -= dataGrid.Columns[LastCol].Width;
          W = dataGrid.Width - total - FixedWidth;
          dataGrid.Columns[LastCol].Width = 
                      Math.Max(W, dataGrid.Columns[LastCol].MinimumWidth);
          PrevWidth = dataGrid.Width;
       }

What it does first is it recalculates all the columns width (if there are visible columns). In the end, we can have rounding errors and still have a scrollbar if the last column is 1 or more pixels wide. So we calculate the exact width needed for the last column in the end.

Please note that I'm only a beginner in C#, so any comments (also negative) or improvements are welcome :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Wilfried Mestdagh

Web Developer

Belgium Belgium

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionAuto-resize datagridview PinmemberMbulelo21:45 31 May '09  
GeneralMy solution with Panel Pinmemberlekhacnhu3:08 7 Jun '07  
QuestionDataGridView in VB.Net Pinmembernathawat17:37 25 Apr '06  
GeneralImprove PinmemberMPulgar5:47 13 Apr '05  
GeneralRe: Improve PinmemberWilfried Mestdagh6:25 13 Apr '05  
GeneralClassification PinmemberMPulgar5:34 13 Apr '05  
GeneralRe: Classification PinmemberWilfried Mestdagh6:26 13 Apr '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120515.1 | Last Updated 10 Apr 2005
Article Copyright 2005 by Wilfried Mestdagh
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid