Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Make Datagrid columns autosize when datagrid is resized

Posted on 2005-03-11
Medium Priority
Last Modified: 2007-12-19
I have a form with a datagrid with 10 cells by 10 cells, set to dock=fill so that when the form is resized by the user, the datagrid resizes and always fills the form.

However, the cells in the datagrid stay the same size so I end up with a large datagrid which has the cells in the top left and a lot of empty space elsewhere.

I want the datagrid cells to resize when the grid resizes to that the 10 columns are always filling the full width of the grid, and the 10 rows are always filling the height of the grid.

Also, it would be cool if the font size increased / decreased with the grid resizing.

Thanks in advance...
Question by:mikesteribar
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 19

Accepted Solution

arif_eqbal earned 2000 total points
ID: 13515439
Here's a code that attempts to do what you want.

    Dim TS As DataGridTableStyle 'We will re-use it so keep it at form level
    Dim ColSizeFactor As Hashtable 'To store Colwidth values

    Private Sub FrmResize_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim DA As New OleDbDataAdapter("Select * From Master", ConStr)
        Dim DT As New DataTable
        DG.DataSource = DT

        ColSizeFactor = New Hashtable

        TS = New DataGridTableStyle
        'Set the dataTable and Fill the Grid as you would
        Dim Col As DataGridTextBoxColumn

        Col = New DataGridTextBoxColumn
        Col.MappingName = "ID"
        Col.HeaderText = "ID"
        Col.Width = 100
        ColSizeFactor.Add("ID", 1) 'Keep a factor for mutliplying the Col Values so that when Form resizes we resize the Col by this factor

        Col = New DataGridTextBoxColumn
        Col.MappingName = "Name"
        Col.HeaderText = "Name"
        Col.Width = 150
        ColSizeFactor.Add("Name", 1.5)

    End Sub

    Private Sub ResizeGrid()
        Dim Fac As Integer = Width \ 2  'I am using Two Columns, You'd use 10 so it will be Divide by 10
        If TS Is Nothing Then Exit Sub
        TS.GridColumnStyles("ID").Width = Fac * ColSizeFactor("ID") '- 100
        TS.GridColumnStyles("Name").Width = Fac * ColSizeFactor("Name") '- 100
        'If you do not subtract the initial value here 100, the scrollbar will be visible as the cols go by this amount outside visible area
        'Now if you subtract then Beware, if form is resized to be very small this value can get negative, so check if "Fac * ColSizeFactor("Name") - 100"   is negative then put some default value, (laziness made me choose the first option)
    End Sub

    Private Sub FrmResize_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        'Call Resize from here
    End Sub

LVL 28

Expert Comment

ID: 13516589
Very nice code :)
LVL 14

Expert Comment

ID: 13517185
Similar code from, in case your datagrid doesn't use GridColumnStyle:

    Dim headerWidth As Integer
    Dim nCols As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim dt As New DataTable("MyTable")

        nCols = 4
        Dim nRows As Integer = 10

        Dim i As Integer
        Dim j As Integer

        For i = 0 To nCols - 1
            dt.Columns.Add(New DataColumn(String.Format("Col{0}", i)))

        For i = 0 To nRows - 1
            Dim dr As DataRow = dt.NewRow()
            For j = 0 To nCols - 1
                dr(j) = String.Format("row{0} col{1}", i, j)

        'add a table style so the columnstyles can be accessed
        Dim ts As DataGridTableStyle = New DataGridTableStyle(True)
        ts.MappingName = "MyTable"

        Me.DataGrid1.DataSource = dt

        Me.headerWidth = Me.DataGrid1.GetCellBounds(0, 0).X


    End Sub

    'make columns equally occupy grid's client area
    Public Sub SizeEqually()
        If Me.DataGrid1.TableStyles("MyTable") Is Nothing Then
        End If

        Dim width As Integer = Me.DataGrid1.Size.Width - Me.headerWidth - 4

        Dim i As Integer
        For i = 0 To nCols - 2
            Me.DataGrid1.TableStyles("MyTable").GridColumnStyles(i).Width = width / nCols

        Me.DataGrid1.TableStyles("MyTable").GridColumnStyles(nCols - 1).Width = width - (nCols - 1) * width / nCols

    End Sub

    'resize after the grid sizes
    Private Sub DataGrid1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.SizeChanged
    End Sub

To change font size, you might need to take a look at this link:

Author Comment

ID: 13517749
Thanks arif_eqbal
Perfect answer :)
LVL 19

Expert Comment

ID: 13523356
Thanks iboutchkine and mikesteribar

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question