Column and Row Resizing

The default appearance vof the DataGridView is a modest improvement over the DataGrid. But with a few quick refinements, you can greatly improve it.

One problem is that the DataGridView gives a default standard width to all columns regardless of their content. As a result, the initial appearance of the bound grid in Figure 15-1 is less than perfect, with the ModelName and Description columns too small for the data they contain.

Fortunately, you can use some powerful automatic resizing functionality that's built into the DataGridView. Your first decision is whether you want to control sizing for the entire control or fine tune individual columns. The following sections explore your options.

Setting an Automatic Resize Mode for the Entire Grid

The simplest approach is to set a resizing behavior that applies to all columns using the AutoSizeColumnsMode property. Your options are shown in Table 15-4.

Table 15-4. AutoSizeColumnsMode Values



None AllCells




DisplayedCellsExceptHeader Fill

The column widths are not adjusted automatically. Extra content is clipped (with an ellipsis added to indicate the missing content). This is the default.

Each column is sized just large enough to fit the largest value, including header cells.

Each column is sized just large enough to fit the largest value, excluding header cells.

Each column is sized just large enough to fit the text in the header.

Similar to AllCells, except the DataGridView only considers the rows that are currently visible at the time the property is set. This option is used to improve performance for large grids. The assumption is that the first subset of values has a fairly representative set of widths.

The same as DisplayedCells, except it doesn't take the header cell into consideration.

Column widths are adjusted so that all columns exactly fill the available DataGridView. If the grid is resized, all the columns change proportionately. You can adjust the MinimumWidth and FillWeight properties of each column to make some columns wider than others.

■Tip Using the AllCells criteria to sort a small table works perfectly well. However, if you have an extremely large table (one with thousands of rows), it introduces a noticeable delay because the width of every value in the table needs to be examined. In these cases, it much more practical to compromise and use the DisplayedCells value instead. This only examines the width of the values in the rows that are currently visible in the DataGridView.

Setting the AutoSizeColumnsMode at any time triggers the DataGridView to resize its columns immediately. Figure 15-6 shows a simple test application in action, changing the grid from AutoSizeColumnsMode.None to AutoSizeColumnsMode.Fill and then to AutoSizeColumnsMode.DisplayCells (in which case the last column is too wide to fit in the grid, and scroll bars are added).

If you use Fill mode, users are still allowed to resize columns (assuming you haven't changed the Resizable property of any DataGridViewColumn objects). When a column is resized, all the following columns are expanded or shrunk proportionately to fit the remaining space. If you resize the last column, all the other columns are resized proportionately.

Datagridview1 Resize
Figure 15-6. Automatic column resizing in the DataGridView

Setting an Automatic Resize Mode for Individual Columns

If you don't want all columns to be resized in the same way, you can adjust the AutoSizeMode property of the DataGridViewColumn object for each column. For example, you could size one column to None and another column to DisplayedCells. (The default value is NotSet, in which case the value is inherited from the DataGridView.AutoSizeColumnsMode property.)

A more interesting scenario occurs if you're using proportional fill. In many cases, you'll want to make some columns larger than others or limit them so they can't shrink beyond a certain minimum. This is easy to accomplish through the FillWeight and MinimumWidth properties.

Initially, the FillWeight of every column is 100. If you set the FillWeight of another column to 200, you create a column that's twice as wide. A FillWeight of 50 is half as large as the default. The FillWeight is only important in a relative sense, unlike the MinimumWidth property, which sets an absolute minimum width in pixels.

Here's an example that configures these details:

// Retrieve the columns you need to work with. DataGridViewColumn colID = dataGridView1.Columns["ProductID"]; DataGridViewColumn colModel = dataGridView1.Columns["ModelName"]; DataGridViewColumn colDesc = dataGridView1.Columns["Description"];

// Give much more weigth to the description. colID.FillWeight = 25; colModel.FillWeight = 25; colDesc.FillWeight = 100;

// However, keep a minimum width that ensures // the first two columns are readable. // Another option in this scenario is to only // assign fill mode to the description column. colID.MinimumWidth = 75; colModel.MinimumWidth = 125; colDesc.MinimumWidth = 100;

■Note Unfortunately, you must configure column properties using code. Although the DataGridView does provide design-time support for modifying the Columns property, unless you're using the automatic data-binding features discussed in Chapter 8 (which aren't suitable for most large-scale projects), you won't be able to add or modify bound columns.

When the user resizes a column, the FillWeight changes. As a result, additional resize operations (on other columns or the whole grid) will work a little differently. For example, if the user expands the first column, it's automatically given a correspondingly larger FillWeight. If the user then resizes the whole form (and by extension, the DataGridView), the first column gets the same larger proportion of space.

There's no need to use Fill mode with every column. You can use Fill mode with just one column. The fill behavior still works the same—the remaining space in the DataGridView is divided among all Fill mode columns.

■Tip If you don't want to use Fill mode for the whole grid, but you want to make sure the DataGridView background isn't visible, you can configure the last column to spring to fill the remaining space. All you need to do is set the AutoSizeColumnsMode of that column to Fill.

Manual Sizing

Automatic resizing is preferred because it's the most flexible approach. However, you can use fixed pixel widths instead. Just set the AutoSizeColumnsMode to None (either for individual columns or for the entire grid) and then set the DataGridViewColumn.Width property with the width in pixels.

User Sizing

By default, the DataGridView allows the user to resize columns that have an AutoSizeColumnsMode of None or Fill. If you are using another option (like DisplayedCells), resizing is disabled. (However, you'll learn how to work around this limitation in the next section.)

If you want to prevent the user from resizing columns or rows altogether, set the AllowUserToResizeRows and AllowUserToResizeColumns properties for your DataGridView to false. You also can restrict the user from resizing individual columns or rows by setting the Resizable property of the corresponding DataGridViewColumn or DataGridViewRow.

Programmatic Resizing

The DataGridView also allows you to trigger autosizing for specific columns or the entire grid by calling one of the following methods:

• AutoResizeColumn()

• AutoResizeColumns()

• AutoResizeRows()

• AutoResizeColumnHeadersHeight()

• AutoResizeRowHeadersWidth()

There are a couple of common reasons that you might choose to use these methods. First of all, there is a possible performance consideration. The DataGridView performs automatic column resizing at several points, including after a column sort and a cell edit. If you've used a resize mode like AllCells, this could be impractically slow. In this case, you might choose to perform your sorting exactly when you want it by calling the appropriate method.

Another reason you might use programmatic resizing is to get around the problem that automatically resized columns don't allow user resizing. If you use a resize mode other than None or Fill, the user won't be able to adjust the column widths. This might be a problem in some situations—for example, if you want the user to be able to collapse a column to see more information without scrolling. To get around this problem, you can leave the default resizing mode to None, but call one of the resizing methods when the form first loads. Here's an example that resizes the third column:

dataGridView1.AutoResizeColumn(2, DataGridViewAutoSizeColumnMode.AllCells);

And here's an example that resizes the whole grid:


Keep in mind that this method needs to be invoked after you bind the data, or it won't have any effect. You also might want to use it after user editing (perhaps in response to an event like DataGridView.CellValueChanged).

Resizing Rows

The DataGridView provides a similar model for resizing rows. Your options are identical to those shown in Table 15-4, and you can resize the height all the rows in the grid or specific rows automatically or manually. The only difference is the name of the properties and methods that you use. For example, the AutoSizeRowsMode property configures automatic resizing for the DataGridView, and the DataGridViewRow.Height property allows you to set a specific pixel height.

There are only three reasons that you'll want to resize a row:

• You've enlarged the font size, so the text is being clipped at the bottom. (Similarly, if you reduce the font size, you might resize the row to get rid of the extra space.)

• You're using a different column type, like an image, and the content extends beyond the bounds of the standard row height.

• You're using wrapped text, and you want to show several lines at once.

The first two options are fairly straightforward. Wrapped text is a little more interesting. It works through the style model described in the next section. The basic approach is that you set the columns that you want to wrap. Then, you set the column width. Finally (and optionally), you use automatic row resizing to heighten the row to fit all the text.

Here's an example that ensures you can always see the lull description text. The Description column is set to use DataGridViewAutoSizeColumnMode.Fill, and the automatic row size adjusts the row height as necessary.

DataGridViewColumn colDesc = dataGridView1.Columns["Description"]; // Give it as much width as possible.

colDesc.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; // Wrap to fit the bounds of the column.

colDesc.DefaultCellStyle.WrapMode = DataGridViewTriState.True; // Use row autosizing to show all the text.

dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells; Figure 15-7 shows how this grid adapts as it is resized.

In this example, the automatic row resizing only takes displayed cells into consideration. Try the same example (included online) with AllCells resizing, and you'll notice more lethargic performance.

■Note You'll see the DataGridViewTriState enumeration used in some places where you might expect to find ordinary Boolean values. The three values are True, False, and NotSet (which inherits values from the containing object). For example, a value of NotSet allows a cell to inherit settings from a row, the row to inherit them from the grid, and so on.

Figure 15-7. Resizing rows to fit wrapped text

Was this article helpful?

+14 -5
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


  • isaias
    Why datagridview row shrink?
    8 years ago
  • benjamin
    How to not resize datagridview row?
    8 years ago
  • Mebrat Iggi
    How to automatically resize a datagridview control in visual studio 10?
    7 years ago
  • Wegahta
    How to resize cell space vba?
    6 years ago
  • henrik
    How can change row height in datagridview in vb 6.0?
    6 years ago
  • william
    How to restrict to resize column of datagrid using vb?
    6 years ago
  • connie
    How to stop auto resizing report columns in visual studio?
    6 years ago
  • Ramona Heath
    How not to resize datagridview column beyond a limit?
    6 years ago
  • Eglantine
    How to deal with grid rows and columns in visual studios?
    6 years ago
  • awet
    How to avoid the extra space in datagrid of vb 6.0?
    4 years ago
  • Konsta
    How to modify height of columns in visual studio?
    4 years ago
  • KIM
    How to make the content to fit into datagrid in visual basic 6.0?
    4 years ago
  • Arthur Moore
    How to resize columns and rows in visual basic datagridview kullanımı?
    4 years ago
  • Ausonio DeRose
    How to resize windows in visual studios 2015?
    4 years ago
  • leonard
    How to set witdh and height to sql server colum in
    4 years ago
  • pentti laaksonen
    How to change fux column font in flex grid vb 6?
    4 years ago
    How to set width and height of image column in datagridview in
    3 years ago
  • william
    How to make colums and rows of equal size in visual basic?
    3 years ago
  • Aedan
    How to change row height in datagird in
    3 years ago
  • juhani haapaniemi
    How to set datagrid column width to fit to content vb 6.0?
    3 years ago
  • Drew
    How to create auto adjustable data grid view rows?
    3 years ago
  • Pamphila
    How to fill datagridview in
    3 years ago
  • Gabriel
    How to get larg value of a grid column in
    3 years ago
  • marissa
    How to resize the cells of in vb databade?
    3 years ago
  • monika
    How to udjust tables in vb?
    3 years ago
  • michael
    How to prevent user from changing column widths visual basic 6?
    3 years ago
    How to resize specific column from database in datgridview?
    3 years ago
  • Simonetta Mazzanti
    How to disable row resizing in datagrid vb6?
    3 years ago
  • Allison Artis
    How to set heidht and with in data section in vb 6.0?
    3 years ago
  • monika k
    How to manually modify width in designer of visual studio?
    2 years ago
  • ghenet
    How to fit texts into the table by visual basic?
    2 years ago
  • Napoleone
    How use i row and j column in case of grid in Vb 6.0?
    2 years ago
  • Veronica Giordano
    How to fit columns to print visual studio?
    2 years ago
  • ahmed
    How to set all propertie of datagridview with code in visual
    2 years ago
    How automatically adjust column width in datagridview
    2 years ago
  • yonas
    How to create table and width setting in datagridview
    2 years ago
  • basso
    How to make tables occupy the whole grid in datagridview visual studio?
    2 years ago
  • sampsa hursti
    How to resize rows of datagridview in properties?
    2 years ago
  • Jana
    How to set cell height of datagridview in in
    2 years ago
  • Isotta
    How to extend row height of datagridview in
    1 year ago
  • Nibs
    How to resize the datagrid row size in vb6?
    1 year ago
  • grimalda
    How to make table rows different sizes in visual studio?
    1 year ago
  • Palmira
    How to set the with of individual column in
    1 year ago
  • teppo
    How to expand one column width in a row visual studio 2015?
    12 months ago
  • amanda
    How to format multicolumn web forms with visual studio?
    12 months ago
    How to roll up rows in visual studio?
    11 months ago
  • dylan
    How to bound cells to grid without space in
    11 months ago
    How to adjust width using table in
    10 months ago
  • ulrich
    How i can make column to be row in visual studio?
    10 months ago
  • declan donaldson
    How to set automatic column size with visual basic?
    8 months ago
  • aleah
    How to make columns in visual studio?
    8 months ago
  • Mewael
    How to change the column width in visual studios to automatic?
    7 months ago
  • philipp
    How to adjust the column width in visual studios windows form?
    6 months ago
  • lotho
    How to resize listview when form is resized in visual studio?
    5 months ago
  • rita
    How to resize grid cell when the font size changes in visual studio?
    4 months ago
  • Diamanda
    How do resize picture in datagridview in vb?
    4 months ago
  • tapio
    How to but lines on other rows in visual studio?
    3 months ago
  • Adaldrida
    5 days ago

Post a comment