如何:设置 Windows 窗体 DataGridView 控件的大小调整模式
下面的过程演示一些常见方案,用于自定义或组合 DataGridView 控件及控件中的特定列可用的大小调整选项。
创建具有固定宽度的列
将 AutoSizeMode 属性设置为 None,Resizable 属性设置为 False,ReadOnly 属性设置为 true,并将 Width 属性设置为适当的值。
Dim idColumn As New DataGridViewTextBoxColumn() idColumn.HeaderText = "ID" idColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None idColumn.Resizable = DataGridViewTriState.False idColumn.ReadOnly = True idColumn.Width = 20
DataGridViewTextBoxColumn idColumn = new DataGridViewTextBoxColumn(); idColumn.HeaderText = "ID"; idColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None; idColumn.Resizable = DataGridViewTriState.False; idColumn.ReadOnly = true; idColumn.Width = 20;
创建可调整大小以适应其内容的列
将 AutoSizeMode 属性设置为基于内容的大小调整模式。
Dim titleColumn As New DataGridViewTextBoxColumn() titleColumn.HeaderText = "Title" titleColumn.AutoSizeMode = _ DataGridViewAutoSizeColumnMode.AllCellsExceptHeader
DataGridViewTextBoxColumn titleColumn = new DataGridViewTextBoxColumn(); titleColumn.HeaderText = "Title"; titleColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader;
为具有不同的大小和重要性的值创建填充模式列
将 DataGridView.AutoSizeColumnsMode 属性设置为 Fill,以便为不重写此值的所有列设置大小调整模式。 将这些列的 FillWeight 属性设置为与内容的平均宽度成一定比例的值。 设置重要列的 MinimumWidth 属性以确保显示部分内容。
dataGridView1.AutoSizeColumnsMode = _ DataGridViewAutoSizeColumnsMode.Fill Dim subTitleColumn As new DataGridViewTextBoxColumn() subTitleColumn.HeaderText = "Subtitle" subTitleColumn.MinimumWidth = 50 subTitleColumn.FillWeight = 100 Dim summaryColumn As new DataGridViewTextBoxColumn() summaryColumn.HeaderText = "Summary" summaryColumn.MinimumWidth = 50 summaryColumn.FillWeight = 200 Dim contentColumn As new DataGridViewTextBoxColumn() contentColumn.HeaderText = "Content" contentColumn.MinimumWidth = 50 contentColumn.FillWeight = 300
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; DataGridViewTextBoxColumn subTitleColumn = new DataGridViewTextBoxColumn(); subTitleColumn.HeaderText = "Subtitle"; subTitleColumn.MinimumWidth = 50; subTitleColumn.FillWeight = 100; DataGridViewTextBoxColumn summaryColumn = new DataGridViewTextBoxColumn(); summaryColumn.HeaderText = "Summary"; summaryColumn.MinimumWidth = 50; summaryColumn.FillWeight = 200; DataGridViewTextBoxColumn contentColumn = new DataGridViewTextBoxColumn(); contentColumn.HeaderText = "Content"; contentColumn.MinimumWidth = 50; contentColumn.FillWeight = 300;
示例
下面的完整代码示例提供了一个演示应用程序,可帮助您了解本主题中介绍的大小调整选项。
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Public Class Form1
Inherits Form
<STAThreadAttribute()> _
Public Shared Sub Main()
Application.Run(New Form1())
End Sub
Private dataGridView1 As New DataGridView()
Public Sub New()
dataGridView1.Dock = DockStyle.Fill
Controls.Add(dataGridView1)
Width *= 2
Text = "DataGridView Sizing Scenarios"
End Sub
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
Dim idColumn As New DataGridViewTextBoxColumn()
idColumn.HeaderText = "ID"
idColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None
idColumn.Resizable = DataGridViewTriState.False
idColumn.ReadOnly = True
idColumn.Width = 20
Dim titleColumn As New DataGridViewTextBoxColumn()
titleColumn.HeaderText = "Title"
titleColumn.AutoSizeMode = _
DataGridViewAutoSizeColumnMode.AllCellsExceptHeader
dataGridView1.AutoSizeColumnsMode = _
DataGridViewAutoSizeColumnsMode.Fill
Dim subTitleColumn As new DataGridViewTextBoxColumn()
subTitleColumn.HeaderText = "Subtitle"
subTitleColumn.MinimumWidth = 50
subTitleColumn.FillWeight = 100
Dim summaryColumn As new DataGridViewTextBoxColumn()
summaryColumn.HeaderText = "Summary"
summaryColumn.MinimumWidth = 50
summaryColumn.FillWeight = 200
Dim contentColumn As new DataGridViewTextBoxColumn()
contentColumn.HeaderText = "Content"
contentColumn.MinimumWidth = 50
contentColumn.FillWeight = 300
dataGridView1.Columns.AddRange(New DataGridViewTextBoxColumn() { _
idColumn, titleColumn, subTitleColumn, _
summaryColumn, contentColumn})
dataGridView1.Rows.Add(New String() {"1", _
"A Short Title", "A Longer SubTitle", _
"A short description of the main point.", _
"The full contents of the topic, with detailed examples."})
MyBase.OnLoad(e)
End Sub
End Class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
[STAThreadAttribute()]
public static void Main()
{
Application.Run(new Form1());
}
private DataGridView dataGridView1 = new DataGridView();
public Form1()
{
dataGridView1.Dock = DockStyle.Fill;
Controls.Add(dataGridView1);
Width *= 2;
Text = "DataGridView Sizing Scenarios";
}
protected override void OnLoad(EventArgs e)
{
DataGridViewTextBoxColumn idColumn =
new DataGridViewTextBoxColumn();
idColumn.HeaderText = "ID";
idColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
idColumn.Resizable = DataGridViewTriState.False;
idColumn.ReadOnly = true;
idColumn.Width = 20;
DataGridViewTextBoxColumn titleColumn =
new DataGridViewTextBoxColumn();
titleColumn.HeaderText = "Title";
titleColumn.AutoSizeMode =
DataGridViewAutoSizeColumnMode.AllCellsExceptHeader;
dataGridView1.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.Fill;
DataGridViewTextBoxColumn subTitleColumn =
new DataGridViewTextBoxColumn();
subTitleColumn.HeaderText = "Subtitle";
subTitleColumn.MinimumWidth = 50;
subTitleColumn.FillWeight = 100;
DataGridViewTextBoxColumn summaryColumn =
new DataGridViewTextBoxColumn();
summaryColumn.HeaderText = "Summary";
summaryColumn.MinimumWidth = 50;
summaryColumn.FillWeight = 200;
DataGridViewTextBoxColumn contentColumn =
new DataGridViewTextBoxColumn();
contentColumn.HeaderText = "Content";
contentColumn.MinimumWidth = 50;
contentColumn.FillWeight = 300;
dataGridView1.Columns.AddRange(new DataGridViewTextBoxColumn[] {
idColumn, titleColumn, subTitleColumn,
summaryColumn, contentColumn });
dataGridView1.Rows.Add(new String[] { "1",
"A Short Title", "A Longer SubTitle",
"A short description of the main point.",
"The full contents of the topic, with detailed examples." });
base.OnLoad(e);
}
}
若要使用此演示应用程序,请执行下列操作:
更改窗体的大小。 观察填充模式列如何在更改宽度的同时保持 FillWeight 属性值所指示的比例。 观察列的 MinimumWidth 如何在窗体太小的时候阻止对列进行更改。
通过用鼠标拖动列分隔符,更改列的大小。 观察为何有些列不能调整大小,为何可调整大小的列不能调整为窄于其最小宽度。
编译代码
此示例需要:
- 对 System 和 System.Windows.Forms 程序集的引用。
有关从 Visual Basic 或 Visual C# 的命令行生成此示例的信息,请参见从命令行生成 (Visual Basic) 或在命令行上使用 csc.exe 生成。 也可以通过将代码粘贴到新项目,在 Visual Studio 中生成此示例。 有关更多信息,请参见 如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例 和 如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例 和 如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例 和 如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例 和 如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例.
请参见
参考
DataGridViewColumn.AutoSizeMode