DataGridTextBoxColumn.GetPreferredSize(Graphics, Object) Method

Definition

Returns the optimum width and height of the cell in a specified row relative to the specified value.

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

Parameters

g
Graphics

A Graphics object used to draw shapes on the screen.

value
Object

The value to draw.

Returns

A Size that contains the dimensions of the cell.

Examples

The following example uses GetPreferredSize to return a Size.

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

};

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

private:
   void GetPreferredSize()
   {
      Graphics^ g;
      g = this->CreateGraphics();
      System::Drawing::Size gridPreferredSize;
      MyGridColumn^ myTextColumn;
      
      // Assuming column 1 of a DataGrid control is a 
      // DataGridTextBoxColumn.
      myTextColumn = dynamic_cast<MyGridColumn^>(dataGrid1->TableStyles[ 0 ]->GridColumnStyles[ 1 ]);
      String^ myVal;
      myVal = "A long string value";
      gridPreferredSize = myTextColumn->GetPrefSize( g, myVal );
      Console::WriteLine( gridPreferredSize );
   }

};
public class Form1: Form
{
 protected DataGrid dataGrid1;

private void GetPreferredSize(){
   Graphics g;
   g = this.CreateGraphics();
   Size gridPreferredSize;
   MyGridColumn myTextColumn;
    // Assuming column 1 of a DataGrid control is a 
    // DataGridTextBoxColumn.
   myTextColumn = (MyGridColumn)
   dataGrid1.TableStyles[0].GridColumnStyles[1];
   string myVal;
   myVal = "A long string value";
   gridPreferredSize = myTextColumn.GetPrefSize(g, myVal);
   Console.WriteLine(gridPreferredSize);
   }
}

public class MyGridColumn:DataGridTextBoxColumn{
   public Size GetPrefSize(Graphics g, string val){
      return this.GetPreferredSize(g, val);
   }
}
Public Class Form1
    Inherits Form
    Protected dataGrid1 As DataGrid
    
    Private Sub GetPreferredSize()
        Dim g As Graphics
        g = Me.CreateGraphics()
        Dim gridPreferredSize As Size
        Dim myTextColumn As MyGridColumn
        ' Assuming column 1 of a DataGrid control is a 
        ' DataGridTextBoxColumn.
        myTextColumn = CType(dataGrid1.TableStyles(0). _
        GridColumnStyles(1), MyGridColumn)
        Dim myVal As String
        myVal = "A long string value"
        gridPreferredSize = myTextColumn.GetPrefSize _
        (g, myVal)
        Console.WriteLine(gridPreferredSize)
    End Sub 
End Class 

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

Remarks

The optimum width and height is calculated by measuring the string size, taking into account its font and attributes, and adding margin values.

Applies to

See also