Export (0) Print
Expand All

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;
    
    

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;
    
    

To create fill-mode columns for values of varying size and importance

  • Set the System.Windows.Forms.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;
    
    

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

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

See Also

Community Additions

ADD
Show:
© 2014 Microsoft