DataGrid

The DataGrid control enables you to display and edit data from many different sources, such as from a SQL database, LINQ query, or any other bindable data source. For more information, see Binding Sources Overview.

Columns can display text, controls, such as a ComboBox, or any other WPF content, such as images, buttons, or any content contained in a template. You can use a DataGridTemplateColumn to display data defined in a template. The following table lists the column types that are provided by default.

Generated Column Type Data Type
DataGridTextColumn String
DataGridCheckBoxColumn Boolean
DataGridComboBoxColumn Enum
DataGridHyperlinkColumn Uri

DataGrid can be customized in appearance, such as cell font, color, and size. DataGrid supports all styling and templating functionality of other WPF controls. DataGrid also includes default and customizable behaviors for editing, sorting, and validation.

The following table lists some of the common tasks for DataGrid and how to accomplish them. By viewing the related API, you can find more information and sample code.

Scenario Approach
Alternating background colors Set the AlternationCount property to 2 or more, and then assign a Brush to the RowBackground and AlternatingRowBackground properties.
Define cell and row selection behavior Set the SelectionMode and SelectionUnit properties.
Customize the visual appearance of headers, cells, and rows Apply a new Style to the ColumnHeaderStyle, RowHeaderStyle, CellStyle, or RowStyle properties.
Set sizing options Set the Height, MaxHeight, MinHeight, Width, MaxWidth, or MinWidth properties. For more information, see Sizing Options in the DataGrid Control.
Access selected items Check the SelectedCells property to get the selected cells and the SelectedItems property to get the selected rows. For more information, see SelectedCells.
Customize end-user interactions Set the CanUserAddRows, CanUserDeleteRows, CanUserReorderColumns, CanUserResizeColumns, CanUserResizeRows, and CanUserSortColumns properties.
Cancel or change auto-generated columns Handle the AutoGeneratingColumn event.
Freeze a column Set the FrozenColumnCount property to 1 and move the column to the left-most position by setting the DisplayIndex property to 0.
Use XML data as the data source Bind the ItemsSource on the DataGrid to the XPath query that represents the collection of items. Create each column in the DataGrid. Bind each column by setting the XPath on the binding to the query that gets the property on the item source. For an example, see DataGridTextColumn.
Title Description
Walkthrough: Display Data from a SQL Server Database in a DataGrid Control Describes how to set up a new WPF project, add an Entity Framework Element, set the source, and display the data in a DataGrid.
How to: Add Row Details to a DataGrid Control Describes how to create row details for a DataGrid.
How to: Implement Validation with the DataGrid Control Describes how to validate values in DataGrid cells and rows, and display validation feedback.
Default Keyboard and Mouse Behavior in the DataGrid Control Describes how to interact with the DataGrid control by using the keyboard and mouse.
How to: Group, Sort, and Filter Data in the DataGrid Control Describes how to view data in a DataGrid in different ways by grouping, sorting, and filtering the data.
Sizing Options in the DataGrid Control Describes how to control absolute and automatic sizing in the DataGrid.

See also