我在 VB.net (2010) 中扩展了这个不错的答案,以处理 DataGridView 控件中文本的自动调整大小。
首先,将函数修改为:
Private Function MeasureTextWidth(ByVal c As Control, ByVal text As String) As Integer
If (c Is DBNull.Value) Then
Return -1
Else
Dim g As Graphics = c.CreateGraphics
Return CInt(Math.Ceiling(g.MeasureString(text, c.Font).Width))
End If
End Function
下一部分,在调用这个函数的软件中是这样的:
With frm_Parameters_FITs
.Show()
With .DataGridView1
.SuspendLayout()
.DataSource = Nothing ' CRITICAL
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
.AllowUserToResizeRows = False
.AllowUserToOrderColumns = True
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.ReadOnly = True
.MultiSelect = False
.RowHeadersVisible = False
.Columns.Clear()
.Rows.Clear()
' setup columns
.Columns.Add("Item", "Item#")
.Columns(0).Width = 11
.Columns(0).SortMode = DataGridViewColumnSortMode.NotSortable
.Columns.Add("Parameter", "gHeaderTitle") ' (ColName, HeaderText)
.Columns(1).Width = 25
.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(1).SortMode = DataGridViewColumnSortMode.NotSortable
.Columns.Add("ParamValue", "gHeaderInfo")
.Columns(2).Width = 40
.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
.Columns(2).SortMode = DataGridViewColumnSortMode.NotSortable
.Columns.Add("Comments", "gHeaderComment")
.Columns(3).Width = 50
.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
.Columns(3).SortMode = DataGridViewColumnSortMode.NotSortable
.Columns.Add("Comments", "Comments")
.Columns(4).Width = 60
.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
.Columns(4).SortMode = DataGridViewColumnSortMode.NotSortable
.ResumeLayout(True)
End With
'Then you can add the data in rows to the cells of the DataGridView:
Dim piIndex As Integer ' this is a row pointer
Dim ColTextWidth(5) As Integer
Dim TextToMeasure As String
Dim IntTextWidthPixels As Integer
With .DataGridView1
For i As Integer = 1 To Globals.giHeaderLines
.Rows.Add() ' increases the rows.count...the last index is (rows.count-1)
piIndex = .Rows.Count - 1 ' pointer to the last row just added
.Rows(piIndex).Cells(0).Value = i.ToString ' puts this text into the col 0 cell
.Rows(piIndex).Cells(0).Style.WrapMode = DataGridViewTriState.True
.Rows(piIndex).Cells(1).Value = gHeaderTitle(i)
.Rows(piIndex).Cells(2).Value = gHeaderInfo(i)
.Rows(piIndex).Cells(3).Value = gHeaderComment(i)
.Rows(piIndex).Cells(4).Value = gHeaderComment(i)
' now determine the correct col width
For j As Integer = 0 To 4
Try
TextToMeasure = .Rows(piIndex).Cells(j).Value
IntTextWidthPixels = MeasureTextWidth(frm_Parameters_FITs.DataGridView1, TextToMeasure)
If IntTextWidthPixels > ColTextWidth(j) Then
ColTextWidth(j) = IntTextWidthPixels
End If
Catch ex As Exception
Debug.Print("Error here in Class_FileIO_FITs. PutDataIntoHeaderDataGrid")
End Try
Next j
Next i
' now reset the cols to the correct width
For j As Integer = 0 To 4
.Columns(j).Width = ColTextWidth(j)
Next j
End With
'make sure the row we added is visible
.DataGridView1.FirstDisplayedScrollingRowIndex = piIndex
.DataGridView1.ClearSelection()
.DataGridView1.Rows(piIndex).Selected = True
.Refresh()
End With
这可以很好地为 DataGridView 控件提供正确宽的列。