'-------------------------------------------------------------------------- ' ' 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
Salut,Hé bien, moi, j'ai trouvé ça super.Je suis débutant aussi.@+Guy FALESSE
Ton code ne marche pas
Ton code se base seulement sur la dernière ligne de la datagrid pour décider de la largeur des colonnes. Ca améliore la visibilité c'est sur, mais si l'enregistrement de cette ligne est beaucoup plus petit ou plus grand que les autres ca rend pas super. Le mieux aurait été de faire une moyenne de la largeur avec toutes les lignes puis y ajouter un coefficient comme tu fais ici. Dommage que je n'ai pas le temps pour le faire, du coup je prend ta source, ca me suffira :)Bonne prog :)
Bonjour,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 NextEnd Function
Public Sub SetGridColumnWidth(grd As MSFlexGrid)Dim i, j As IntegerDim lngLongestLen As LongDim sLongestString As StringFor 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) + 10NextEnd Sub
Se souvenir du profil
Mot de passe oublié ? / Activation de compteCréer un compte