Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Whoever designed the datagrid should be shot
Message
Information générale
Forum:
ASP.NET
Catégorie:
WebForms
Divers
Thread ID:
00796470
Message ID:
00796928
Vues:
11
Hi Cathi,
Here is the C# code I have converted from the first link. http://www.syncfusion.com/faq/winforms/search/877.asp
I have made several modifications including comments and removal of hardcoding that will allow portability and added value in the community. Enjoy.
    Public Sub AutoSizeCol(ByRef dg As DataGrid, _
                  Optional ByVal col As Integer = 0, _
                  Optional ByVal iTable As Integer = 0)

        Dim swidth As Single = 0
        Dim iPadding As Int16 = 8                                                   ' Set the padding size

        Dim g As Graphics = Graphics.FromHwnd(dg.Handle)                            ' Get a handle to the grid
        Dim sf As StringFormat = New StringFormat(StringFormat.GenericTypographic)  ' We need this for the measurements
        Dim size As SizeF                                                           ' Stores an ordered pair of floating-point numbers

        Try
            Dim i As Integer
            For i = 0 To 5000000                                                    ' Loop till EOF then fall out to finally
                size = g.MeasureString(dg(i, col).ToString(), dg.Font, 500, sf)     ' Get the 'size' of the text
                If size.Width > swidth Then                                         ' If this is the biggest save it
                    swidth = size.Width                                             ' Keep collecting the largest size
                End If
            Next
        Catch                                                                       ' Just run like -while not EOF-
        Finally                                                                     ' Finally :) 
            g.Dispose()                                                             ' House cleaning
            dg.TableStyles(iTable).GridColumnStyles(col).Width = swidth + iPadding  ' Padding 
        End Try
    End Sub
* :( There should be access to the internal method of the datagrid to do this. It is available to the control when you dblclick the edge of the column header without this code.
~Joe Johnston USA

"If ye love wealth better than liberty, the tranquility of servitude better than the animated contest of freedom, go home from us in peace. We ask not your counsel or arms. Crouch down and lick the hands which feed you. May your chains set lightly upon you, and may posterity forget that ye were our countrymen."
~Samuel Adams

Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform