Autoresize a DataViewGrid to never have horizontal scrollbar

來源:互聯網
上載者:User

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:

   <span class="cs-keyword">int</span> PrevWidth;<br><span class="cs-keyword">public</span> Form1()<br>   {<br>       InitializeComponent();<br>       PrevWidth = dataGrid.Width;<br>   }<br><br><span class="cs-keyword">private</span> <span class="cs-keyword">void</span> Form1_ResizeEnd(<span class="cs-keyword">object</span> sender, EventArgs e)<br>   {<br>       dataGrid_Resize(dataGrid, <span class="cs-keyword">ref</span> PrevWidth);<br>   }

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

Collapse
       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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.