DataGridColumnStyle.GetPreferredHeight(Graphics, Object) Method

Definition

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

C#
protected internal abstract int GetPreferredHeight(System.Drawing.Graphics g, object value);

Parameters

g
Graphics

A Graphics object.

value
Object

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

Returns

The height used for auto resizing a cell.

Examples

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

C#
public class Form1: Form
{
 protected DataGrid dataGrid1;

private void GetHeight(){
    MyGridColumn myGridColumn;
    // Get a DataGridColumnStyle of a DataGrid control.
    myGridColumn = (MyGridColumn) dataGrid1.TableStyles[0].
    GridColumnStyles["CompanyName"];
    // 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);
   }
}

Remarks

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

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 10

See also