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:
00797033
Vues:
14
Hi Joe,

Thanks for posting this!!

>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.
-----------------------------------------

Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform