begin process at 2008 09 06 20:17:47
1 237 931 membres
313 nouveaux aujourd'hui
14 314 membres club

Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

DATAGRID: AJUSTER LA LARGEURS DES COLONNES


Information sur la source

Description

Bonjour!
Petite fonction bien pratique.
Elle ajuste la largeur des colonne de votre datagrid au contenus de celle-ci.
J'en ai pas trouvé sur le site alors je l'ajoute!

C'est ma première source alors on reste gentil hein? :D

Source

  • '--------------------------------------------------------------------------
  • '
  • ' 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
'--------------------------------------------------------------------------
'
' 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
  • signaler à un administrateur
    Commentaire de GuyFALESSE le 24/04/2005 15:19:50

    Salut,

    Hé bien, moi, j'ai trouvé ça super.
    Je suis débutant aussi.

    @+

    Guy FALESSE

  • signaler à un administrateur
    Commentaire de gamani06 le 14/07/2005 12:04:08

    Ton code ne marche pas

  • signaler à un administrateur
    Commentaire de tthiouwz le 13/11/2005 23:17:08

        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 :)

  • signaler à un administrateur
    Commentaire de lambdacrash le 08/08/2006 13:07:49

    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
        Next
    End Function

  • signaler à un administrateur
    Commentaire de scorpion1970 le 17/06/2008 10:40:28

    Public Sub SetGridColumnWidth(grd As MSFlexGrid)
    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

Ajouter un commentaire

Pub



Appels d'offres

CalendriCode

Septembre 2008
LMMJVSD
1234567
891011121314
15161718192021
22232425262728
2930     

Boutique

Boutique de goodies CodeS-SourceS