Click here to Skip to main content
Licence 
First Posted 8 Apr 2004
Views 46,951
Bookmarked 16 times

DataGrid Column adjustments during resizing form (using ParamArray)

By | 14 Apr 2004 | Article
Automatically handle to adjust grid widths

Introduction

AutoSize of columns based on the user resizing of the form is a not good way to do it. But, through this article, it is going to explain how can resize a grid columns using a single function.

"GridResizeColumns" Procedure

The "GridResizeColumns" function will adjust the columns while you resizing the VB form. This procedure will support both VB6 and VB.NET applications.

Parameters

There are four parameters are listed:

1. DataGridView - Control - This will be Your Grid Control Name

2. FixedTotalWidth - Double - This will be your total Fixed Grid Width (Say for eg. eg., you may want to keep a check box in a grid that needs to be fixed size. )

3. StartCol - Integer - This identifies which column do you want to start to resize

4. ColumnWidthInPercent() - ParamArray & Double - You can assign each column percentage size.

Sample Code

The following the sample code of the "GridResizeColumns" procedure. You can also download full source code for this.

 
Private Sub GridResizeColumns(ByVal DataGridView As Control,_<BR>                             ByVal FixedTotalWidth As Double, _<BR>                             ByVal StartCol As Integer,_<BR>                          ByVal ParamArray ColumnWidthInPercent() As Double)

Dim TotalGridWidth As Long

Dim intIndex As Integer

Dim PercentColWidth As Integer

On Error GoTo ErrHandler

With DataGridView1

'Exclusing Fixed column width

TotalGridWidth = DataGridView.Width - FixedTotalWidth

.ScrollBars = flexScrollBarVertical

For Each PercentColWidth In ColumnWidthInPercent

.Columns(StartCol).Width = (PercentColWidth / 100) * TotalGridWidth

StartCol = StartCol + 1

Next

.Refresh()

End With

Exit Sub

ErrHandler:

Exit Sub

End Sub

Example:

GridResizeColumns(MyDataGridView1, 0, 0, 25, 25, 25, 25)

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

Binoy R



United States United States

Member

Binoy is a software developer of Information Technology Division in Memphis,TN.

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
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 15 Apr 2004
Article Copyright 2004 by Binoy R
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid