Edit

Share via


How to: Set the Sizing Modes of the Windows Forms DataGridView Control

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

  • 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

  • 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

  • Set the DataGridView.AutoSizeColumnsMode property to Fill to set the sizing mode for all columns that do not override this value. Set the FillWeight properties of the columns to values that are proportional to their average content widths. 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. Observe how the fill-mode columns change their widths while retaining the proportions indicated by the FillWeight property values. 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:

  • References to the System and System.Windows.Forms assemblies.

See also