[]
Represents a zoom policy for DataGridView type control.
public class DataGridViewZoomPolicy : ZoomPolicy
This zoom policy will try to zoom all fonts and row height, column width.
If a DataGridView has many rows or columns, change all cell's font,row's height or column's width many bring performance problem. Change this property value to false can solve the performance problem.
The following code example shows how to use DataGridViewZoomPolicy class.
You can copy the code to a exsit Windows Forms appliction. And set the form in the sample code as start form.
If you find a licenses error when run this code. Try to drag a C1Zoom component to a any Form from tool box can generate the licenses information to this project.
public class DataGridViewZoomPolicyDemo : Form
{
private DataGridView _dataGridView;
private CheckBox _enableCheckBox;
private CheckBox _fontCheckBox;
private CheckBox _columnWidthCheckBox;
private CheckBox _rowHeightCheckBox;
private TextBox _targetTypeTextBox;
private ListBox _defaultZoomPolicyListBox;
private GcZoom _gcZoom1 = new GcZoom();
private DataGridViewZoomPolicy _zoomPolicy = new DataGridViewZoomPolicy();
public DataGridViewZoomPolicyDemo()
{
InitializeComponent();
// DataGridView control should to change default font
_dataGridView.ColumnHeadersDefaultCellStyle.Font = new Font(this.Font.FontFamily, this.Font.Size);
_dataGridView.RowsDefaultCellStyle.Font = new Font(this.Font.FontFamily, this.Font.Size);
_gcZoom1.Target = this;
// Set DataGridView's ZoomPolicy object
_gcZoom1.ZoomPolicies.Add(_zoomPolicy);
GetTargetType();
GetDefaultZoomPolicy();
_enableCheckBox.CheckedChanged += _enableCheckBox_CheckedChanged;
_fontCheckBox.CheckedChanged += _fontCheckBox_CheckedChanged;
_columnWidthCheckBox.CheckedChanged += _columnWidthCheckBox_CheckedChanged;
_rowHeightCheckBox.CheckedChanged += _rowHeightCheckBox_CheckedChanged;
}
void GetTargetType()
{
_targetTypeTextBox.Text = _zoomPolicy.TargetType.Name;
}
void GetDefaultZoomPolicy()
{
// Get GcZoom component build in zoompolicy of all
foreach (ZoomPolicy item in _gcZoom1.DefaultZoomPolicies)
{
_defaultZoomPolicyListBox.Items.Add(item.ToString());
}
}
void _enableCheckBox_CheckedChanged(object sender, EventArgs e)
{
_gcZoom1.ZoomFactor = 1f;
_zoomPolicy.Enabled = _enableCheckBox.Checked;
_fontCheckBox.Enabled = _enableCheckBox.Checked;
_columnWidthCheckBox.Enabled = _enableCheckBox.Checked;
_rowHeightCheckBox.Enabled = _enableCheckBox.Checked;
}
void _fontCheckBox_CheckedChanged(object sender, EventArgs e)
{
_gcZoom1.ZoomFactor = 1f;
_zoomPolicy.ZoomCellStyleFont = _fontCheckBox.Checked;
}
void _columnWidthCheckBox_CheckedChanged(object sender, EventArgs e)
{
_gcZoom1.ZoomFactor = 1f;
_zoomPolicy.ZoomColumnWidth = _columnWidthCheckBox.Checked;
}
void _rowHeightCheckBox_CheckedChanged(object sender, EventArgs e)
{
_gcZoom1.ZoomFactor = 1f;
_zoomPolicy.ZoomRowHeight = _rowHeightCheckBox.Checked;
}
private void InitializeComponent()
{
_dataGridView = new DataGridView();
_dataGridView.Location = new Point(20, 20);
_dataGridView.Size = new Size(400, 500);
DataGridViewColumn column1 = new DataGridViewColumn();
column1.CellTemplate = new DataGridViewTextBoxCell();
column1.HeaderText = "Column 1";
_dataGridView.Columns.Add(column1);
DataGridViewColumn column2 = new DataGridViewColumn();
column2.CellTemplate = new DataGridViewTextBoxCell();
column2.HeaderText = "Column 2";
_dataGridView.Columns.Add(column2);
DataGridViewColumn column3 = new DataGridViewColumn();
column3.CellTemplate = new DataGridViewTextBoxCell();
column3.HeaderText = "Column 3";
_dataGridView.Columns.Add(column3);
_enableCheckBox = new CheckBox();
_enableCheckBox.Text = "Enabled";
_enableCheckBox.Size = new Size(120, 20);
_enableCheckBox.Location = new Point(450, 20);
_enableCheckBox.Checked = true;
_fontCheckBox = new CheckBox();
_fontCheckBox.Text = "ZoomCellStyleFont";
_fontCheckBox.Size = new Size(120, 20);
_fontCheckBox.Location = new Point(450, 50);
_fontCheckBox.Checked = true;
_columnWidthCheckBox = new CheckBox();
_columnWidthCheckBox.Text = "ZoomColumnWidth";
_columnWidthCheckBox.Size = new Size(120, 20);
_columnWidthCheckBox.Location = new Point(450, 80);
_columnWidthCheckBox.Checked = true;
_rowHeightCheckBox = new CheckBox();
_rowHeightCheckBox.Text = "ZoomRowHeight";
_rowHeightCheckBox.Size = new Size(120, 20);
_rowHeightCheckBox.Location = new Point(450, 110);
_rowHeightCheckBox.Checked = true;
_targetTypeTextBox = new TextBox();
_targetTypeTextBox.ReadOnly = true;
_targetTypeTextBox.Size = new Size(120, 20);
_targetTypeTextBox.Location = new Point(450, 140);
_defaultZoomPolicyListBox = new ListBox();
_defaultZoomPolicyListBox.Size = new Size(300, 200);
_defaultZoomPolicyListBox.Location = new Point(450, 170);
this.Controls.Add(_dataGridView);
this.Controls.Add(_enableCheckBox);
this.Controls.Add(_fontCheckBox);
this.Controls.Add(_columnWidthCheckBox);
this.Controls.Add(_rowHeightCheckBox);
this.Controls.Add(_targetTypeTextBox);
this.Controls.Add(_defaultZoomPolicyListBox);
this.Size = new Size(800, 600);
}
}
Public Class DataGridViewZoomPolicyDemo
Inherits Form
Private _dataGridView As DataGridView
Private _enableCheckBox As CheckBox
Private _fontCheckBox As CheckBox
Private _columnWidthCheckBox As CheckBox
Private _rowHeightCheckBox As CheckBox
Private _targetTypeTextBox As TextBox
Private _defaultZoomPolicyListBox As ListBox
Private _gcZoom1 As New GcZoom()
Private _zoomPolicy As New DataGridViewZoomPolicy()
Public Sub New()
InitializeComponent()
' DataGridView control should to change default font
_dataGridView.ColumnHeadersDefaultCellStyle.Font = New Font(Me.Font.FontFamily, Me.Font.Size)
_dataGridView.RowsDefaultCellStyle.Font = New Font(Me.Font.FontFamily, Me.Font.Size)
_gcZoom1.Target = Me
' Set DataGridView's ZoomPolicy object
_gcZoom1.ZoomPolicies.Add(_zoomPolicy)
GetTargetType()
GetDefaultZoomPolicy()
AddHandler _enableCheckBox.CheckedChanged, AddressOf _enableCheckBox_CheckedChanged
AddHandler _fontCheckBox.CheckedChanged, AddressOf _fontCheckBox_CheckedChanged
AddHandler _columnWidthCheckBox.CheckedChanged, AddressOf _columnWidthCheckBox_CheckedChanged
AddHandler _rowHeightCheckBox.CheckedChanged, AddressOf _rowHeightCheckBox_CheckedChanged
End Sub
Private Sub GetTargetType()
_targetTypeTextBox.Text = _zoomPolicy.TargetType.Name
End Sub
Private Sub GetDefaultZoomPolicy()
' Get GcZoom component build in zoompolicy of all
For Each item As ZoomPolicy In _gcZoom1.DefaultZoomPolicies
_defaultZoomPolicyListBox.Items.Add(item.ToString())
Next
End Sub
Private Sub _enableCheckBox_CheckedChanged(sender As Object, e As EventArgs)
_gcZoom1.ZoomFactor = 1.0F
_zoomPolicy.Enabled = _enableCheckBox.Checked
_fontCheckBox.Enabled = _enableCheckBox.Checked
_columnWidthCheckBox.Enabled = _enableCheckBox.Checked
_rowHeightCheckBox.Enabled = _enableCheckBox.Checked
End Sub
Private Sub _fontCheckBox_CheckedChanged(sender As Object, e As EventArgs)
_gcZoom1.ZoomFactor = 1.0F
_zoomPolicy.ZoomCellStyleFont = _fontCheckBox.Checked
End Sub
Private Sub _columnWidthCheckBox_CheckedChanged(sender As Object, e As EventArgs)
_gcZoom1.ZoomFactor = 1.0F
_zoomPolicy.ZoomColumnWidth = _columnWidthCheckBox.Checked
End Sub
Private Sub _rowHeightCheckBox_CheckedChanged(sender As Object, e As EventArgs)
_gcZoom1.ZoomFactor = 1.0F
_zoomPolicy.ZoomRowHeight = _rowHeightCheckBox.Checked
End Sub
Private Sub InitializeComponent()
_dataGridView = New DataGridView()
_dataGridView.Location = New Point(20, 20)
_dataGridView.Size = New Size(400, 500)
Dim column1 As New DataGridViewColumn()
column1.CellTemplate = New DataGridViewTextBoxCell()
column1.HeaderText = "Column 1"
_dataGridView.Columns.Add(column1)
Dim column2 As New DataGridViewColumn()
column2.CellTemplate = New DataGridViewTextBoxCell()
column2.HeaderText = "Column 2"
_dataGridView.Columns.Add(column2)
Dim column3 As New DataGridViewColumn()
column3.CellTemplate = New DataGridViewTextBoxCell()
column3.HeaderText = "Column 3"
_dataGridView.Columns.Add(column3)
_enableCheckBox = New CheckBox()
_enableCheckBox.Text = "Enabled"
_enableCheckBox.Size = New Size(120, 20)
_enableCheckBox.Location = New Point(450, 20)
_enableCheckBox.Checked = True
_fontCheckBox = New CheckBox()
_fontCheckBox.Text = "ZoomCellStyleFont"
_fontCheckBox.Size = New Size(120, 20)
_fontCheckBox.Location = New Point(450, 50)
_fontCheckBox.Checked = True
_columnWidthCheckBox = New CheckBox()
_columnWidthCheckBox.Text = "ZoomColumnWidth"
_columnWidthCheckBox.Size = New Size(120, 20)
_columnWidthCheckBox.Location = New Point(450, 80)
_columnWidthCheckBox.Checked = True
_rowHeightCheckBox = New CheckBox()
_rowHeightCheckBox.Text = "ZoomRowHeight"
_rowHeightCheckBox.Size = New Size(120, 20)
_rowHeightCheckBox.Location = New Point(450, 110)
_rowHeightCheckBox.Checked = True
_targetTypeTextBox = New TextBox()
_targetTypeTextBox.[ReadOnly] = True
_targetTypeTextBox.Size = New Size(120, 20)
_targetTypeTextBox.Location = New Point(450, 140)
_defaultZoomPolicyListBox = New ListBox()
_defaultZoomPolicyListBox.Size = New Size(300, 200)
_defaultZoomPolicyListBox.Location = New Point(450, 170)
Me.Controls.Add(_dataGridView)
Me.Controls.Add(_enableCheckBox)
Me.Controls.Add(_fontCheckBox)
Me.Controls.Add(_columnWidthCheckBox)
Me.Controls.Add(_rowHeightCheckBox)
Me.Controls.Add(_targetTypeTextBox)
Me.Controls.Add(_defaultZoomPolicyListBox)
Me.Size = New Size(800, 600)
End Sub
End Class
Name | Description |
---|---|
DataGridViewZoomPolicy() |
Name | Description |
---|---|
TargetType | Gets the type indicates which control type can use this policy. |
ZoomCellStyleFont | Gets or sets a value indicating whether zoom cell style's font. |
ZoomColumnWidth | Gets or sets a value indicating whether resize column's width. |
ZoomRowHeight | Gets or sets a value indicating whether resize row's height. |
Name | Description |
---|---|
ZoomBounds(Control, ZoomBoundsInfo) | Zoom the control bounds. |
ZoomFont(Control, ZoomFontInfo) | Zoom the control font. |