DataGridColumnStyle.GetPreferredHeight(Graphics, Object) Method


When overridden in a derived class, gets the height used for automatically resizing columns.

protected public:
 abstract int GetPreferredHeight(System::Drawing::Graphics ^ g, System::Object ^ value);
protected internal abstract int GetPreferredHeight (System.Drawing.Graphics g, object value);
abstract member GetPreferredHeight : System.Drawing.Graphics * obj -> int
Protected Friend MustOverride Function GetPreferredHeight (g As Graphics, value As Object) As Integer



A Graphics object.


An object value for which you want to know the screen height and width.



The height used for auto resizing a cell.


The following code example uses the GetPreferredHeight method to return the height of a cell.

public ref class MyGridColumn: public DataGridTextBoxColumn
   int GetPrefHeight( Graphics^ g, String^ thisString )
      return this->GetPreferredHeight( g, thisString );


public ref class Form1: public Form
   DataGrid^ dataGrid1;

   void GetHeight()
      MyGridColumn^ myGridColumn;
      // Get a DataGridColumnStyle of a DataGrid control.
      myGridColumn = dynamic_cast<MyGridColumn^>(dataGrid1->TableStyles[ 0 ]->GridColumnStyles[ "CompanyName" ]);
      // Create a Graphics object.
      Graphics^ g = this->CreateGraphics();
      Console::WriteLine( myGridColumn->GetPrefHeight( g, "A string" ) );


public class Form1: Form
 protected DataGrid dataGrid1;

private void GetHeight(){
    MyGridColumn myGridColumn;
    // Get a DataGridColumnStyle of a DataGrid control.
    myGridColumn = (MyGridColumn) dataGrid1.TableStyles[0].
    // Create a Graphics object.
    Graphics g = this.CreateGraphics();
    Console.WriteLine(myGridColumn.GetPrefHeight(g, "A string"));

public class MyGridColumn:DataGridTextBoxColumn{
   public int GetPrefHeight(Graphics g, string thisString){
      return this.GetPreferredHeight(g,thisString);
Public Class Form1
    Inherits Form
    Protected dataGrid1 As DataGrid

    Private Sub GetHeight()
        Dim myGridColumn As MyGridColumn
        ' Get a DataGridColumnStyle of a DataGrid control.
        myGridColumn = CType(dataGrid1.TableStyles(0). _
        GridColumnStyles("CompanyName"), myGridColumn)
        ' Create a Graphics object.
        Dim g As Graphics = Me.CreateGraphics()
        Console.WriteLine(myGridColumn.GetPrefHeight(g, "A string"))
    End Sub 

End Class 

Public Class MyGridColumn
Inherits DataGridTextBoxColumn
   public Function GetPrefHeight (g As Graphics , _
   thisString As String ) As Integer
      return me.GetPreferredHeight(g,thisString)
   End Function
End Class


The GetPreferredSize will usually be invoked from a mouse down event to resize a column's height for a long string.

Applies to

See also