DataGridColumnStyle.GetPreferredSize(Graphics, Object) Method

Definition

When overridden in a derived class, gets the width and height of the specified value. The width and height are used when the user navigates to DataGridTableStyle using the DataGridColumnStyle.

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

Parameters

g
Graphics

A Graphics object.

value
Object

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

Returns

A Size that contains the dimensions of the cell.

Examples

The following code example uses the GetPreferredSize method to return the optimum size for a value.

public ref class MyGridColumn: public DataGridTextBoxColumn
{
public:
   Size GetPrefSize( Graphics^ g, String^ thisString )
   {
      return this->GetPreferredSize( g, thisString );
   }

};

public ref class Form1: public Form
{
protected:
   DataGrid^ dataGrid1;

private:
   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();
      System::Drawing::Size s = myGridColumn->GetPrefSize( 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].
    GridColumnStyles["CompanyName"];
    // Create a Graphics object.
    Graphics g = this.CreateGraphics();
    Size s =myGridColumn.GetPrefSize(g, "A string");
 }
}

public class MyGridColumn:DataGridTextBoxColumn{
   public Size GetPrefSize(Graphics g, string thisString){
      return this.GetPreferredSize(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()
      Dim s As Size = myGridColumn.GetPrefSize(g, "A string")
   End Sub 

End Class 

Public Class MyGridColumn
Inherits DataGridTextBoxColumn
   public Function GetPrefSize(g As Graphics , _
   thisString As String ) As Size
      return me.GetPreferredSize(g,thisString)
   End Function
End Class

Remarks

Use GetPreferredSize to determine the width a column should resize to, given a particular string or numeral.

Applies to

See also