方法: Windows フォーム DataGridView コントロールのサイズ変更モードを設定するHow to: Set the Sizing Modes of the Windows Forms DataGridView Control

次の手順では、DataGridView コントロールおよびコントロール内の特定の列に対して使用できるサイズ変更オプションをカスタマイズまたは組み合わせる一般的なシナリオを示します。The following procedures demonstrate some common scenarios that customize or combine the sizing options available for the DataGridView control and for specific columns in a control.

固定幅の列を作成するにはTo create a fixed-width column

  • AutoSizeMode プロパティを None に、Resizable プロパティを False に、ReadOnly プロパティを true に、Width プロパティに適切な値をそれぞれ設定します。Set the AutoSizeMode property to None, the Resizable property to False, the ReadOnly property to true, and the Width property to an appropriate value.

    DataGridViewTextBoxColumn idColumn =
        new DataGridViewTextBoxColumn();
    idColumn.HeaderText = "ID";
    idColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
    idColumn.Resizable = DataGridViewTriState.False;
    idColumn.ReadOnly = true;
    idColumn.Width = 20;
    
    Dim idColumn As New DataGridViewTextBoxColumn()
    idColumn.HeaderText = "ID"
    idColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None
    idColumn.Resizable = DataGridViewTriState.False
    idColumn.ReadOnly = True
    idColumn.Width = 20
    

コンテンツに合わせてサイズが調整される列を作成するにはTo create a column that adjusts its size to fit its content

  • AutoSizeMode プロパティを、コンテンツに基づくサイズ変更モードに設定します。Set the AutoSizeMode property to a content-based sizing mode.

    DataGridViewTextBoxColumn titleColumn =
        new DataGridViewTextBoxColumn();
    titleColumn.HeaderText = "Title";
    titleColumn.AutoSizeMode =
        DataGridViewAutoSizeColumnMode.AllCellsExceptHeader;
    
    Dim titleColumn As New DataGridViewTextBoxColumn()
    titleColumn.HeaderText = "Title"
    titleColumn.AutoSizeMode = _
        DataGridViewAutoSizeColumnMode.AllCellsExceptHeader
    

サイズと重要度が異なる値に対してフィル モードの列を作成するにはTo create fill-mode columns for values of varying size and importance

  • DataGridView.AutoSizeColumnsMode プロパティを Fill に設定し、この値をオーバーライドしないすべての列のサイズ変更モードを設定します。Set the DataGridView.AutoSizeColumnsMode property to Fill to set the sizing mode for all columns that do not override this value. 列の FillWeight プロパティを、平均のコンテンツ幅に比例した値に設定します。Set the FillWeight properties of the columns to values that are proportional to their average content widths. 重要な列の MinimumWidth プロパティを設定して、コンテンツを部分的に表示できるようにします。Set the MinimumWidth properties of important columns to ensure partial content display.

    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.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
    

Example

ここで説明したサイズ変更オプションを理解するために役立つデモ アプリケーションを次の完全なコード例に示します。The following complete code example provides a demonstration application that can help you understand the sizing options described in this topic.

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

このデモ アプリケーションは、次のようにして使用します。To use this demonstration application:

  • フォームのサイズを変更します。Change the size of the form. FillWeight プロパティ値で指定した比率を維持しながら、フィル モードの列の幅が変化する様子を確認します。Observe how the fill-mode columns change their widths while retaining the proportions indicated by the FillWeight property values. フォームが小さすぎる場合に、列の MinimumWidth より狭くは変更されない様子を確認します。Observe how a column's MinimumWidth prevents it from changing when the form is too small.

  • マウスで列の区分線をドラッグして、列のサイズを変更します。Change the column sizes by dragging the column dividers with the mouse. 一部の列ではサイズ変更ができない様子、およびサイズ変更可能な列でも幅を最小幅よりも狭くできない様子を確認します。Observe how some columns cannot be resized, and how resizable columns cannot be made narrower than their minimum widths.

コードのコンパイルCompiling the Code

この例で必要な要素は次のとおりです。This example requires:

  • System アセンブリおよび System.Windows.Forms アセンブリへの参照。References to the System and System.Windows.Forms assemblies.

関連項目See also