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

x
?
Solved

Make Datagrid columns autosize when datagrid is resized

Posted on 2005-03-11
5
Medium Priority
?
506 Views
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...
0
Comment
Question by:mikesteribar
[X]
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
5 Comments
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 2000 total points
ID: 13515439
Hi
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
        DA.Fill(DT)
        DA.Dispose()
        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
        TS.GridColumnStyles.Add(Col)

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

        DG.TableStyles.Add(TS)
    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)
        DG.TableStyles.Clear()
        DG.TableStyles.Add(TS)
    End Sub

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

0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 13516589
arif_eqbal
Very nice code :)
0
 
LVL 14

Expert Comment

by:amyhxu
ID: 13517185
Similar code from www.syncfusion.com, 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)))
        Next

        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)
            Next
            dt.Rows.Add(dr)
        Next


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

        Me.DataGrid1.DataSource = dt

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

        SizeEqually()

    End Sub

    'make columns equally occupy grid's client area
    Public Sub SizeEqually()
        If Me.DataGrid1.TableStyles("MyTable") Is Nothing Then
            Return
        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
        Next

        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
        SizeEqually()
    End Sub

To change font size, you might need to take a look at this link:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/wnf_CustDataGrid.asp
0
 

Author Comment

by:mikesteribar
ID: 13517749
Thanks arif_eqbal
Perfect answer :)
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 13523356
Thanks iboutchkine and mikesteribar
0

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