Public Class BrowseData
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    Public Sub New(ByVal dataSet As DataSet, ByVal tableName As String)
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        Me.Text = tableName
        Dim tableStyle As DataGridTableStyle
        tableStyle = New DataGridTableStyle()
        tableStyle.MappingName = dataSet.Tables(0).TableName
        ' make the dataGrid use our new tablestyle and bind it to our table
        DataGrid1.TableStyles.Clear()
        DataGrid1.TableStyles.Add(tableStyle)
        DataGrid1.DataSource = dataSet.Tables(0)

        AutoSizeColumns(Me.DataGrid1)
    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.DataGrid1 = New System.Windows.Forms.DataGrid()
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'DataGrid1
        '
        Me.DataGrid1.BackgroundColor = System.Drawing.SystemColors.Window
        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid1.Name = "DataGrid1"
        Me.DataGrid1.Size = New System.Drawing.Size(292, 266)
        Me.DataGrid1.TabIndex = 0
        '
        'BrowseData
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.DataGrid1})
        Me.Name = "BrowseData"
        Me.Text = "BrowseData"
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Sub AutoSizeColumns(ByRef dataGrid As DataGrid)
        Const MaxWidth = 500
        Const MaxRows = 100
        Const Padding = 20

        Dim g As Graphics = Graphics.FromHwnd(dataGrid.Handle)
        Dim sf As StringFormat = New StringFormat(StringFormat.GenericTypographic)
        Dim size As SizeF ' Size struct of X,Y floating

        Dim width As Single
        Dim row, col As Integer

        Dim totalRows As Integer = CType(dataGrid.DataSource, DataTable).Rows.Count
        Dim countRows As Integer = IIf(totalRows > MaxRows, MaxRows, totalRows) ' Avg of first 100 rows only
        Dim countColumns As Integer = CType(dataGrid.DataSource, DataTable).Columns.Count
        Dim caption As String

        For col = 0 To countColumns - 1
            ' Check the caption's width first
            caption = CType(dataGrid.DataSource, DataTable).Columns(col).Caption.ToString
            size = g.MeasureString(caption, dataGrid.HeaderFont, MaxWidth, sf)
            width = size.Width + Padding
            ' Loop all rows to get the widest
            For row = 0 To countRows - 1
                size = g.MeasureString(dataGrid(row, col).ToString, dataGrid.Font, MaxWidth, sf)
                If (size.Width + Padding > width) Then
                    width = size.Width + Padding
                End If
            Next
            ' Apply width
            dataGrid.TableStyles(0).GridColumnStyles(col).Width = CType(width, Integer)
        Next

        g.Dispose()

    End Sub

End Class
