?
Solved

Make Datagrid columns autosize when datagrid is resized

Posted on 2005-03-11
5
Medium Priority
?
513 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
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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
Watch the video to learn how one can deal with PST file corruption issue with an outstanding Kernel for Outlook PST Repair Tool easily. Using this tool, non-technical users can swiftly perform the repair process to restore their essential data witho…

599 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