DataGridTextBoxColumn::GetPreferredSize Method (Graphics^, Object^)

 
Use BaseTrue

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

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

public protected:
virtual Size GetPreferredSize(
	Graphics^ g,
	Object^ value
) override

Parameters

g
Type: System.Drawing::Graphics^

A Graphics object used to draw shapes on the screen.

value
Type: System::Object^

The value to draw.

Return Value

Type: System.Drawing::Size

A Size that contains the dimensions of the cell.

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

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 );
   }

};

.NET Framework
Available since 1.1
Return to top
Show: