Gets the optimum width and height of a cell given a specific value to contain.
[Visual Basic] Overrides Public Function GetPreferredSize( _ ByVal g As Graphics, _ ByVal value As Object _ ) As Size [C#] public override Size GetPreferredSize( Graphics g, object value ); [C++] public: override Size GetPreferredSize( Graphics* g, Object* value ); [JScript] public override function GetPreferredSize( g : Graphics, value : Object ) : Size;
A Size that contains the drawing information for the cell.
The GetPreferredSize method allows you to resize the column based on the value displayed. For example, if a cell contains an especially large value, you can use the GetPreferredSize method to return the optimum size of the cell based on the value. The optimum size takes into account not only the size of the string, but also the font used to display it.
The following example returns the width of a given string using the GetPreferredSize method.
[Visual Basic]
Private Sub GetNatExtent() Dim editCol As DataGridColumn ' Set the variable to a column that contains a string (not a Boolean column). Set editCol = DataGrid1.GridColumns(1) ' You must create a Graphics object for the method to use. Dim g As Graphics Set g = Me.CreateGraphics ' Create a variable for the string to test. Dim longString As String longString = "A very long, long string." Dim colWidth As Integer ' Get the width of the string with it's current font. colWidth = editCol.GetNaturalExtents(g, longString).Width Debug.Print colWidth ' Change the font size. Set DataGrid1.Font = New System.Drawing.Font("Times Roman", 12!) ' Get the width of the string with it's new font. colWidth = editCol.GetNaturalExtents(g, longString).Width Debug.Print colWidth ' Set the width of the column with the new width editCol.Width = colWidth End Sub
DataGridBoolColumn Class | DataGridBoolColumn Members | System.WinForms Namespace | DataGridColumn | DataGrid | GetMinimumHeight | GetPreferredHeight