DataGridView Property Notes

Dot Net Perls

You are using DataGridView in your .NET program and there are hundreds of confusing properties you can change in the designer. Here are notes on DataGridView properties made from my work with DataGridViews in Windows.

DataGridView properties

Introduction

The first task in making your DataGridView functional and attractive is using the Visual Studio designer and setting properties. After you set these properties, you can usually just plug in your data easily.

Properties

In Visual Studio, first select the View -> Toolbox option, and then double-click on DataGridView as shown above. Next, right click on the box and select Properties. Here is a review of some of the options you can set. Some important options are bolded. Column properties are not included here.

MultiSelect Property (DataGridView)

Accessibility options These are important only depending on the target of your application. Accessibility is very difficult to get right.

AlternatingRowsDefaultCellStyle This allows you to use two different colors alternating in your DataGridView.

BackgroundColor Set this to a symbolic color, such as Window or AppWorkspace, for a configuration-dependent style.

BorderStyle Has three options: None, FixedSingle and Fixed3D. For Vista-targeted applications, FixedSingle looks good.

CellBorderStyle Has several options, althrough I have found none of them really result in a good appearance.

ColumnHeadersBorderStyle This changes the appearance of the border on the column headers. As a reminder, the column headers are the gray boxes at the top.

ColumnHeadersDefaultCellStyle Allows you to specify a default cell style for the column headers. I have not found this useful, as I usually just predefine columns.

ColumnHeadersHeight Specify the height in pixels of the column headers row. This could rarely be useful.

ColumnHeadersVisible Set this to False if you don't want the headers to appear. This will result in a simpler grid appearance. For most data-driven apps, however, the column headers are very important.

Cursor This functions the same as it does on other Windows Forms controls.

DefaultCellStyle Click on the ellipsis on the right of this row in the Properties pane, and you can change properties such as BackColor, Font, ForeColor, SelectionBackColor, SelectionForeColor, as well as Alignment and WrapMode. This lets you specify all aspects of the font and text layout.

EnableHeadersVisualStyle You can try toggling this option but in my experience it almost always makes the appearance look worse when it is set to False.

GridColor Set this to a symbolic color enum, such as ControlDark, or WindowText. These special color enums adapt to the user's system.

RightToLeft This specifies the behavior of the DataGridView based on the user's localization. Many languages in the world are RTL, not LTR, and depending on your market, this could be critical. I have no experience in this area.

RowHeadersBorderStyle Use this to specify the style of the row headers. As a reminder, the row headers are the boxes on the left, which I hide in this tutorial.

RowHeadersDefaultCellStyle Like ColumnHeadersDefaultCellStyle above, this allows you to specify the text and alignment properties of the row headers.

RowHeadersVisible Set this to False to hide the row headers.

RowsDefaultCellStyle Here you can specify the default cell style for Rows collections. You can access Rows on the DataGridView through the Rows property.

RowTemplate I haven't used this. It would allow you to specify a "template" row that the other rows copy.

ShowCellErrors ShowCellToolTips ShowEditingIcon ShowRowErrors These properties specify the errors and icons that are displayed on the cells. These are useful for data entry applications, which aren't covered in this tutorial.

AllowUserToAddRows AllowUserToDeleteRows AllowUserToOrderColumns AllowUserToResizeColumns AllowUserToResizeRows These properties let you set what the user is allowed to do. For example, you can set AllowUserToAddRows to false, and the bottom empty cell will disappear. This is because it no longer is needed.

ClipboardCopyMode This lets you specify how Copy, Ctrl-C, is handled in your DataGridView. You can set this to one of four values. The values are Disable, which prevents all copying; and three more options with specific characteristics.

ColumnHeadersHeightSizeMode You can specify whether you want the system to dynamically resize the height of the headers here.

EditMode Allows you to specify how the user can indicate she wants to start editing the DataGridView. The tutorial here doesn't cover editing.

MultiSelect Here you can indicate whether only one part is able to be selected at once. Depending on the other properties set, this can apply to cells, rows, or columns.

ReadOnly Set ReadOnly to true when you do not require any editing to take place. This is for when the DataGridView is only used for viewing, not editing or data entry.

RowHeadersWidthSizeMode Row headers are the boxes on the left, and this option lets you change their sizing mode. I haven't worked carefully with this property.

SelectionMode SelectionMode is an important property that allows you to specify what units are selected when you click on the DataGridView. The options include FullRowSelect, CellSelect, FullColumnSelect, RowHeaderSelect, and ColumnHeaderSelect.

VirtualMode The word 'Virtual' when used in the DataGridView context refers to custom grid management methods that you have developed. It applies both for data-management and also painting.

Data -> DataBindings DataMember DataSource In the tutorial here, we use the DataSource property to display the data onto the screen. It allows you to completely avoid manipulating rows individually.

Layout -> AutoSizeColumnsMode AutoSizeRowsMode These two options let you specify less specific sizings modes for your DataGridView. For DataGridViews where the exact layout isn't critical, these are very useful.

Misc. -> Columns Very important for defining custom column rules. See Step 8 and its following information for more details.

What properties to start with. First, you can expand and anchor the DataGridView so it fills your window. Often, you will want to hide the row headers in Appearance -> RowHeadersVisible. Sometimes you should change the SelectionMode.

Column properties

The Column properties on DataGridView provide a template for you to format your data. You add the Column properties, which are independent of the data but style it when it is adapted.

Appearance -> DefaultCellStyle You can change the default appearance of cells in the column. This lets you specify BackColor, Font, ForeColor, SelectionBackColor, and SelectionForeColor. Other important properties include Alignment, Padding, and WrapMode. Additionally, you can specify Behavior.

HeaderText This text appears in the header. We already assigned it in the initial Columns dialog boxes.

ToolTipText This allows you to specify some tool tip text. These are the little labels that show under the mouse pointer.

Visible Use this to completely hide this column. This is useful for when you want to remove an entire column from the DataGridView that is in the database.

Behavior -> ContextMenuStrip Assign this property to any ContextMenuStrip you have in your Windows Forms program.

MaxInputLength Specify the maximum number of characters to be inserted into this cell. This can prevent overly large text from being inserted later.

ReadOnly Use to prevent user edits entirely.

Resizable You can specify whether each column could be resized, seperately.

SortMode The type of sorting that is performed when the column header is clicked. There are three options: NotSortable, Automatic, and Programmatic. NotSortable specifies that the order will never be changed. Automatic is adequate for most programs in my experience. Finally, Programmatic means that you must sort the column manually in your C# code.

Data -> DataPropertyName As seen above, this "links" the database column to your DataGridView.

Design -> ColumnType There are several different options for column types. They are DataGridViewTextBoxColumn, which is used for this tutorial. Others are DataGridViewButtonColumn, which allows you to insert push buttons, DataGridViewCheckBoxColumn for check boxes, DataGridViewComboBoxColumn, and DataGridViewImageColumn.

Layout -> AutoSizeMode AutoSizeMode indicates that the column be resized to fit its contents in a way determined by Microsoft. This reduces your control over layout.

DividerWidth You can set how wide the dividers, meaning the lines between the columns, are drawn.

FillWeight Fill weight refers to the relative proportions allotted to this column. If you have a really high fill weight, the column will have the most aggressive fill mode.

Frozen Visual Studio says: "Indicates whether a column will move when the user scrolls the DataGridView horizontally."

MinimumWidth Sometimes it is useful to specify a column never shrink too much.

Width How wide, in pixels, you want the column to be.

Start with Column properties. Here we note how you can start changing the columns in your DataGridView. You can set HeaderText, which indicates what text to substitute for the data column name from your SQL database. If your column name in the database is ColumnSam1, then set the DataPropertyName to "ColumnSam1" without the quotes.

More

Programming tip

There are more features to the DataGridView you can use. You can set alternating row colors. An example of this is in the DataGridView tutorial. It is really valuable to set Resizable, which adds an extra layer of polish because it prevents the program from getting in an unusable configuration.

Summary

In this article, we saw my notes on DataGridView as I studied MSDN and worked on several implementations of the control. There are more materials available that offer walkthroughs and other neat tips. This site has a DataGridView category.

Windows Forms