begin process at 2012 02 16 21:53:59
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Control

 > DATAGRID: AJUSTER LA LARGEURS DES COLONNES

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



 Sources de la même categorie

Source avec Zip COMMUNICATION MODBUS MASTER par sergelapointe
Source avec Zip Source avec une capture DÉPLACEMENT AVEC FLÈCHES DANS UN PAVÉ DE TEXTBOX 9X9 DYNAMIQ... par EhJoe
Source avec Zip Source avec une capture Source .NET (Dotnet) CONTROLSTARS EN RÉPONSE À JAKNIGHT007 par bigboss9
Source avec Zip Source avec une capture Source .NET (Dotnet) CALENDRIER ANNUEL NORME ISO par Prog1001
Source avec Zip Source avec une capture Source .NET (Dotnet) CONTROLE STARS par jaknight007

Commentaires et avis

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

Commentaire de gamani06 le 14/07/2005 12:04:08

Ton code ne marche pas

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

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

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

Commentaire de maitrealino le 21/12/2011 22:15:37

je trouve ça vraiment super! mille mercis

 Ajouter un commentaire




Nos sponsors


Sondage...

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 1,154 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales