DataGridColumnStyle.GetPreferredSize 方法
在派生类中被重写时,将获取指定值的宽度和高度。在用户定位到使用 DataGridColumnStyle 的 DataGridTableStyle 时将使用该宽度和高度。
**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)
语法
声明
Protected Friend MustOverride Function GetPreferredSize ( _
g As Graphics, _
value As Object _
) As Size
用法
Dim g As Graphics
Dim value As Object
Dim returnValue As Size
returnValue = Me.GetPreferredSize(g, value)
protected internal abstract Size GetPreferredSize (
Graphics g,
Object value
)
protected public:
virtual Size GetPreferredSize (
Graphics^ g,
Object^ value
) abstract
protected abstract Size GetPreferredSize (
Graphics g,
Object value
)
protected internal abstract function GetPreferredSize (
g : Graphics,
value : Object
) : Size
参数
- g
一个 Graphics 对象。
- value
一个对象值,该值是需要知道屏幕高度和宽度的值。
返回值
一个 Size,它包含单元格的尺寸。
备注
在给定特定字符串或数字的情况下,使用 GetPreferredSize 确定列应调整到的宽度。
示例
下面的代码示例使用 GetPreferredSize 方法来返回值的最佳大小。
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
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 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 extends Form
{
protected DataGrid dataGrid1;
private void GetHeight()
{
MyGridColumn myGridColumn;
// Get a DataGridColumnStyle of a DataGrid control.
myGridColumn = (MyGridColumn)(dataGrid1.get_TableStyles().
get_Item(0).get_GridColumnStyles().get_Item("CompanyName"));
// Create a Graphics object.
Graphics g = this.CreateGraphics();
Size s = myGridColumn.GetPrefSize(g, "A string");
} //GetHeight
} //Form1
public class MyGridColumn extends DataGridTextBoxColumn
{
public Size GetPrefSize(Graphics g, String thisString)
{
return this.GetPreferredSize(g, thisString);
} //GetPrefSize
} //MyGridColumn
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
请参见
参考
DataGridColumnStyle 类
DataGridColumnStyle 成员
System.Windows.Forms 命名空间
GetPreferredHeight
GetMinimumHeight