Avatar of neil438
neil438

asked on 

How do I change column widths in a .net datagrid

I am creating a .net form which uses a datagrid.  The datagrid uses a datatable to get its data from.  There are 5 columns in the datatable and I want to individual size each of the column widths based upon maximum data size.  I would also like to be able to wordwrap the column headings so that the column heading can be more descriptive of the data in the columns.
Option Explicit On
Public Class yardstorageform
    ' declaration of yardstortab as a datatable
    ' this will be used to connect the datagrid
    Private yardstoretab As Data.DataTable
    Dim downstore As Integer
    Public yard10 As Decimal
    Public yard1 As Decimal
    Public yard_dec As Decimal
    Public totyards As Decimal
    Public yard10ltg As Decimal
    Public yard1ltg As Decimal
    Public yard_decltg As Decimal
    Public totyardsltg As Decimal
    Public team_in_possws As String
    Public team_yardws As String
    Public teamltg_yardws As String
    Public actyardline As Decimal
    Public actltgyardline As Decimal
    Public yards_to_1st As Integer
    Private Sub yardstorageform_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
        ' Retrieves info from downandyardline form
        If downandyardline.RadioButton1.Checked = True Then
            downstore = 1
        Else
            If downandyardline.RadioButton3.Checked = True Then
                downstore = 2
            Else
                If downandyardline.RadioButton2.Checked = True Then
                    downstore = 3
                Else
                    downstore = 4
                End If
            End If
        End If
        'Converts yardline and line to gain info into real numbers
        yard10 = downandyardline.tenyardcombo.Text
        yard1 = downandyardline.oneyardcombo.Text
        yard_dec = downandyardline.decimalcombo.Text
        totyards = (yard10 + yard1 + yard_dec)
        team_in_possws = downandyardline.teampossesion.Text
        team_yardws = downandyardline.teamcombo.Text
        teamltg_yardws = downandyardline.ltgteam.Text
        yard10ltg = downandyardline.ltgtenyardcombo.Text
        yard1ltg = downandyardline.ltgoneyardcombo.Text
        yard_decltg = downandyardline.ltgdecimalcombo.Text
        totyardsltg = (yard10ltg + yard1ltg + yard_decltg)
        ' Calculates yards to 1st down
        If team_in_possws = team_yardws Then
            actyardline = totyards
        Else
            actyardline = (50 - totyards) + 50
        End If
        If downstore = 1 Then
            If team_in_possws = teamltg_yardws Then
                actltgyardline = totyardsltg
            Else
                actltgyardline = (50 - totyardsltg) + 50
            End If
        End If
        yards_to_1st = actltgyardline - actyardline
        ' set up a Data Row
        Dim MyRow As Data.DataRow
        If downandyardline.curr_row_cnt = downandyardline.row_cnt Then
            ' adds data to the table
            MyRow = yardstoretab.NewRow()
            ' index on MyRow is the cell number
            MyRow(0) = downstore
            MyRow(1) = team_in_possws
            MyRow(2) = team_yardws
            MyRow(3) = totyards
            MyRow(4) = yards_to_1st
            ' add row to table - index is the row number
            yardstoretab.Rows.InsertAt(MyRow, downandyardline.row_cnt)
            ' display the datagrid
            yardstoregrid.DataSource = yardstoretab
            yardstoregrid.Refresh()
            downandyardline.row_cnt = (downandyardline.row_cnt + 1)
        End If
    End Sub
    Private Sub yardstorageform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' instantiates the data table in the storage form load 
        ' to maintain state
 
 
        yardstoretab = New Data.DataTable
        ' code used to load table styles.  This allows 
        ' formatting on yardstoregrid
        Dim tableStyle As New DataGridTableStyle
        ' next line is to set up column format
        Dim Col1 As New DataGridTextBoxColumn
        Col1.Width = 15
        ' next line matches to the column name in yardstoretab
        Col1.MappingName = "D"
        ' defines the column header for this column
        Col1.HeaderText = "D"
        Dim Col2 As New DataGridTextBoxColumn
        Col2.Width = 70
        ' next line matches to the column name in yardstoretab
        Col2.MappingName = "Team in Poss"
        ' defines the column header for this column
        Col2.HeaderText = "Team in Poss"
        Dim Col3 As New DataGridTextBoxColumn
        Col3.Width = 70
        ' next line matches to the column name in yardstoretab
        Col3.MappingName = "Team Yardline"
        ' defines the column header for this column
        Col3.HeaderText = "Team Yardline"
        Dim Col4 As New DataGridTextBoxColumn
        Col4.Width = 30
        ' next line matches to the column name in yardstoretab
        Col4.MappingName = "Line"
        ' defines the column header for this column
        Col4.HeaderText = "Line"
        Dim Col5 As New DataGridTextBoxColumn
        Col5.Width = 30
        ' next line matches to the column name in yardstoretab
        Col5.MappingName = "To 1st"
        ' defines the column header for this column
        Col5.HeaderText = "To 1st"
        ' bind the gridcolumnstyle to the tablestyle
        tableStyle.GridColumnStyles.Add(Col1)
        tableStyle.GridColumnStyles.Add(Col2)
        tableStyle.GridColumnStyles.Add(Col3)
        tableStyle.GridColumnStyles.Add(Col4)
        tableStyle.GridColumnStyles.Add(Col5)
        ' bind the tablestyle to yardstoregrid
        yardstoregrid.TableStyles.Add(tableStyle)
        yardstoretab.Columns.Add(New Data.DataColumn("D", GetType(String)))
        yardstoretab.Columns.Add(New Data.DataColumn("Team in Poss", GetType(String)))
        yardstoretab.Columns.Add(New Data.DataColumn("Team Yardline", GetType(String)))
        yardstoretab.Columns.Add(New Data.DataColumn("Line", GetType(String)))
        yardstoretab.Columns.Add(New Data.DataColumn("To 1st", GetType(String)))
 
    End Sub
 
    Private Sub retrnbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles retrnbutton.Click
        downandyardline.Show()
        Me.Hide()
    End Sub
 
    Private Sub yardstoregrid_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yardstoregrid.CurrentCellChanged
        ' yardstoregrid.PreferredColumnWidth = 10
    End Sub
End Class

Open in new window

Smartphone Programming.NET Programming

Avatar of undefined
Last Comment
neil438

8/22/2022 - Mon