DataGridTextBoxColumn.GetPreferredHeight Method (Graphics, Object)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Use BaseTrue

Gets the height to be used in for automatically resizing columns.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

protected internal override int GetPreferredHeight(
	Graphics g,
	object value


Type: System.Drawing.Graphics

A Graphics object used to draw shapes on the screen.

Type: System.Object

The value to draw.

Return Value

Type: System.Int32

The height the cells automatically resize to.

The following example uses the GetPreferredHeight to return the height a column is automatically resized to.

public class Form1: Form
 protected DataGrid dataGrid1;

private void GetPreferredHeight(){
   Graphics g;
   g = this.CreateGraphics();
   int gridPreferredHeight;
   MyGridColumn myTextColumn;
    // Assuming column 1 of a DataGrid control is a 
    // DataGridTextBoxColumn.
   myTextColumn = (MyGridColumn)
   string myVal;
   myVal = "A long string value";
   gridPreferredHeight= myTextColumn.GetPrefHeight(g, myVal);

public class MyGridColumn:DataGridTextBoxColumn{
   public int GetPrefHeight(Graphics g, string val){
      return this.GetPreferredHeight(g, val);

.NET Framework
Available since 1.1
Return to top