共用方式為


HOW TO:設定 Windows Form DataGridView 控制項的縮放模式

更新:2007 年 11 月

下列程序示範一些常見的案例,這些案例自訂或結合了 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 中建置此範例。

請參閱

參考

DataGridView

DataGridViewColumn.AutoSizeMode

DataGridViewAutoSizeColumnMode

DataGridViewColumn.Resizable

DataGridViewColumn.ReadOnly

DataGridViewColumn.Width

DataGridViewColumn.FillWeight

DataGridViewColumn.MinimumWidth