0/5 (6 avis)
Snippet vu 11 547 fois - Téléchargée 32 fois
'-------------------------------------------------------------------------- ' ' Ajuste la largeur des colonnes au contenus de la grid ' ' Parametres: ' oDataGrid -> le nom de la Datagrid à redimensionner ' oForm -> le nomn de la form ' ' Note: Si ColumnHeading est plus large que CellWidth, HeadingWidth est utilisé ' 'Pour l'utilisé vous appelez dans votre code comme ceci : ' DatagridColumnAutoResize nomForm.nomDataGrid, nomForm '-------------------------------------------------------------------------- Public Sub DatagridColumnAutoResize(ByRef oDataGrid As DataGrid, _ ByRef oForm As Form) Dim i As Integer, iMax As Integer Dim t As Integer, tMax As Integer Dim iWidth As Integer Dim vBMark As Variant Dim aWidth As Variant Dim cText As String Dim oFont As Font On Error Resume Next oFont = oForm.Font oForm.Font = oDataGrid.Font iMax = oDataGrid.Columns.Count - 1 ReDim aWidth(iMax) For i = 0 To iMax 'init maxwidth holder aWidth(i) = 0 Next tMax = oDataGrid.VisibleRows - 1 For t = 0 To tMax 'nombres de lignes vBMark = oDataGrid.GetBookmark(t) For i = 0 To iMax 'nombres de colonnes cText = oDataGrid.Columns(i).CellText(vBMark) iWidth = oForm.TextWidth(cText) If iWidth + ((12 * Len(cText)) + 220) > aWidth(i) Then 'quelques ajustements manuels aWidth(i) = iWidth + ((12 * Len(cText)) + 220) End If If t = 0 Then 's'occupe des headers iWidth = oForm.TextWidth(oDataGrid.Columns(i).Caption) If iWidth + ((12 * Len(cText)) + 220) > aWidth(i) Then aWidth(i) = iWidth + ((12 * Len(cText)) + 220) End If End If Next Next For i = 0 To iMax ' Assigne la nouvelle largeur de colonne oDataGrid.Columns(i).Width = aWidth(i) Next oForm.Font = oFont End Sub
24 avril 2005 à 15:19
Hé bien, moi, j'ai trouvé ça super.
Je suis débutant aussi.
@+
Guy FALESSE
14 juil. 2005 à 12:04
13 nov. 2005 à 23:17
Bonne prog :)
8 août 2006 à 13:07
Je propose plutôt ceci, c'est un morceau de code fait sur le coin du bureau. Il ne gère pas la longueur des headers.
Public Function dataGridAutoResize(oDataGrid As Object)
Dim i, j As Integer
Dim dataGridNumRows As Integer
Dim dataGridNumCols As Integer
Dim vBMark As Variant
Dim cellTextLength As Long
dataGridNumRows = oDataGrid.VisibleRows - 1
dataGridNumCols = oDataGrid.Columns.Count - 1
For j = 0 To dataGridNumCols
cellTextLength = 0
For i = 0 To dataGridNumRows
vBMark = DataGrid1.GetBookmark(i)
If cellTextLength < Len(oDataGrid.Columns(j).CellText(vBMark)) Then
cellTextLength = Len(oDataGrid.Columns(j).CellText(vBMark))
End If
Next
oDataGrid.Columns(j).Width = 100 * cellTextLength + 70
Next
End Function
17 juin 2008 à 10:40
Dim i, j As Integer
Dim lngLongestLen As Long
Dim sLongestString As String
For j = 0 To grd.Cols - 1
sLongestString = ""
lngLongestLen = 0
For i = 0 To grd.Rows - 1
If Len(grd.TextMatrix(i, j)) > lngLongestLen Then
lngLongestLen = Len(grd.TextMatrix(i, j))
sLongestString = grd.TextMatrix(i, j)
End If
Next
grd.ColWidth(j) = grd.Parent.TextWidth(sLongestString) + 10
Next
End Sub
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.