Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

Automatic Column Sizing

One of the DataGridView control’s new features is its ability to automatically calculate the width of the columns to fit the content of the columns based on several different criteria. Like many of the grid’s features, all you need to do to take advantage of this feature is to set the appropriate property on a given column—and then the grid does the rest. Specifically, the property that takes care of this for you is the AutoSizeMode property of the DataGridViewColumn class. By setting this property to one of the enumerated values of the DataGridViewAutoSizeColumnMode enumeration shown in Table 6.5, you can drive how to set the width of columns in the grid.

DataGridView AutoSizeMode Values

Value

How the Column Width Is Calculated

NotSet

By the value set on the AutoSizeColumnsMode property at the grid level. This is the default value.

None

Set explicitly by setting the column’s Width property.

ColumnHeader

By the width of the content in the header cell only.

AllCellsExceptHeader

By the width of the widest cell in the grid, whether it is displayed or not, but the header cell content size is ignored.

AllCells

By the width of all cells in the column, including those not displayed and the header cell content.

DisplayedCellsExceptHeader

Based only on displayed cells in the column, ignoring the width of the header cell content.

DisplayedCells

By the width of the content in displayed cells, including the header cell.

Fill

Automatically calculated to fill the displayed content area of the grid so the contents can be viewed without scrolling. The actual value used depends on the mode of the other columns in the grid, and their MinimumWidth and FillWeight properties. If all the columns in the grid are set to Fill mode, and their minimum width requirements are met, then the columns will each have equal width, filling the grid, but not requiring any horizontal scrolling.

One of the most useful values is AllCells. I recommend that this be your default, unless you see a performance hit from using it for large data sets or if you have some cell values that will be very long. This setting ensures that the content of cells never wraps. Additionally, remember to set the FormattingApplied property on the event argument to the CellFormatting event if you are dynamically populating cell values. Otherwise, setting the AutoSizeMode to one of the row values will result in an infinite loop.

As a simple example of using this feature, the following code modifies the code from Listing 6.1 to set the column width of the Full Name computed column:

newColIndex = m_AuthorsGrid.Columns.Add("FullName", "Full Name");
m_AuthorsGrid.Columns[newColIndex].AutoSizeMode =
   DataGridViewAutoSizeColumnMode.AllCells;

The Fill mode is very powerful for automatically maximizing the use of the grid real estate, but it can be a little complicated to understand. Basically, if you set the mode for all columns to Fill, each of the columns will have their width set equally, and the columns will fill the grid boundary with no horizontal scrollbar needed. If the MinimumWidth property of any column set to Fill mode is wider than the width that was computed using the fill algorithm, then the MinimumWidth value will be used instead, and the other columns just end up being narrower so that they all still fit in the grid without a horizontal scrollbar. If the MinimumWidth values of multiple columns make it so that all columns cannot be displayed, then the columns that cannot be displayed will be set to their minimum width value, and a scrollbar will be displayed. The default value for minimum width is only 5 pixels, so you will definitely want to set a more sensible MinimumWidth value when working with Fill mode.

Each column also has a FillWeight property, which takes effect when that column’s AutoSizeMode is set to Fill. The FillWeight can be thought of as the percentage of the remaining available grid width that the individual column will take up compared to other columns that are set to Fill. It is a weight instead of a percentage, though, because you can use values that don’t add up to 100. For example, suppose you wanted to display the CustomerID, CompanyName, and ContactName columns from the Northwind Customers table in a grid. The following code sets the column width of the CustomerID column to 75 pixels, and then sets the remaining two columns to Fill mode with weights of 10 and 20, respectively.

public Form1()
{

   InitializeComponent();
   m_CustomersGrid.Columns["CustomerID"].Width = 75;
   m_CustomersGrid.Columns["CompanyName"].AutoSizeMode =
      DataGridViewAutoSizeColumnMode.Fill;
   m_CustomersGrid.Columns["CompanyName"].FillWeight = 10;
   m_CustomersGrid.Columns["ContactName"].AutoSizeMode =
      DataGridViewAutoSizeColumnMode.Fill;
   m_CustomersGrid.Columns["ContactName"].FillWeight = 20;

}

As a result, the remaining two columns occupy 33 percent and 67 percent of the remaining grid width, respectively, after the CustomerID column has taken up its fixed width space. Figure 6.2 illustrates this.

06fig02.jpg

Figure 6.2 Columns Using AutoSizeMode of Fill and FillWeights

  • + Share This
  • 🔖 Save To Your Account