Click here to Skip to main content
15,909,953 members
Articles / Programming Languages / C#
Article

Autoresize a DataViewGrid to never have horizontal scrollbar

Rate me:
Please Sign up or sign in to vote.
3.91/5 (7 votes)
10 Apr 20051 min read 85.7K   50   7
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:

C#
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).

C#
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


Written By
Web Developer
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAuto-resize datagridview Pin
Mbulelo31-May-09 21:45
Mbulelo31-May-09 21:45 
GeneralMy solution with Panel Pin
lekhacnhu7-Jun-07 3:08
lekhacnhu7-Jun-07 3:08 
I use a Panel and put DataGridView inside this Panel, change Dock property is Fill. Now, resize panel as your wish Smile | :)
QuestionDataGridView in VB.Net Pin
nathawat25-Apr-06 17:37
nathawat25-Apr-06 17:37 
GeneralImprove Pin
MPulgar13-Apr-05 5:47
MPulgar13-Apr-05 5:47 
GeneralRe: Improve Pin
Wilfried Mestdagh13-Apr-05 6:25
Wilfried Mestdagh13-Apr-05 6:25 
GeneralClassification Pin
MPulgar13-Apr-05 5:34
MPulgar13-Apr-05 5:34 
GeneralRe: Classification Pin
Wilfried Mestdagh13-Apr-05 6:26
Wilfried Mestdagh13-Apr-05 6:26 

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

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