Bonjour,
j'ai un datagrid sur mon formulaire.
j'aimerai que les colonnes de ce dernier se dimensionne automatiquement au contenu des cellules, au chargement de mon formulaire.
Merci de votre aide
Damien
![]()
Bonjour,
j'ai un datagrid sur mon formulaire.
j'aimerai que les colonnes de ce dernier se dimensionne automatiquement au contenu des cellules, au chargement de mon formulaire.
Merci de votre aide
Damien
![]()
Il s'agit d'un Datagrid
Merci
bonjour,
je suis intéressée pour connaitre la réponse pour un datagridview...
Merci
Bonjour,
je viens de trouver la solution a mon probléme:
Private sub Chargment
' prend la taille de la colonne en fonction du contenu
Dim newwidth As Integer = LongestField(DataGridOracle, DataSetGen, TableGen, "DESCRIPTION")
' créerun nouveau style
Dim ts As New DataGridTableStyle
ts.MappingName = TableGen
DataGridOracle.TableStyles.Clear()
DataGridOracle.TableStyles.Add(ts)
' assigne la largeur de la colonne
DataGridOracle.TableStyles(TableGen).GridColumnStyles("DESCRIPTION").Width = newwidth
end sub
Private Function LongestField(ByVal DataGridGeneral As DataGrid, ByVal ds As DataSet, ByVal TableName As String, ByVal ColumnName As String) As Integer
Dim maxlength As Integer = 0
Dim g As Graphics = DataGridGeneral.CreateGraphics()
' Take width of one blank space and add to the new width of the Column.
Dim offset As Integer = Convert.ToInt32(Math.Ceiling(g.MeasureString(" ", DataGridGeneral.Font).Width))
Dim i As Integer = 0
Dim intaux As Integer
Dim straux As String
Dim tot As Integer = ds.Tables(TableName).Rows.Count
For i = 0 To (tot - 1)
straux = ds.Tables(TableName).Rows(i)(ColumnName).ToString()
' Get the width of Current Field String according to the Font.
intaux = Convert.ToInt32(Math.Ceiling(g.MeasureString(straux, DataGridGeneral.Font).Width))
If (intaux > maxlength) Then
maxlength = intaux
End If
Next
Return maxlength + offset
End Function
Donc ce code redimensionne le datagrid en fonction du contenu.
Partager