DataGrid

.NET Framework (current version)
 

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 TypeData Type
DataGridTextColumnString
DataGridCheckBoxColumnBoolean
DataGridComboBoxColumnEnum
DataGridHyperlinkColumnUri

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.

ScenarioApproach
Alternating background colorsSet the AlternationIndex property to 2 or more, and then assign a Brush to the RowBackground and AlternatingRowBackground properties.
Define cell and row selection behaviorSet the SelectionMode and SelectionUnit properties.
Customize the visual appearance of headers, cells, and rowsApply a new Style to the ColumnHeaderStyle, RowHeaderStyle, CellStyle, or RowStyle properties.
Set sizing optionsSet the Height, MaxHeight, MinHeight, Width, MaxWidth, or MinWidth properties. For more information, see Sizing Options in the DataGrid Control.
Access selected itemsCheck 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 interactionsSet the CanUserAddRows, CanUserDeleteRows, CanUserReorderColumns, CanUserResizeColumns, CanUserResizeRows, and CanUserSortColumns properties.
Cancel or change auto-generated columnsHandle the AutoGeneratingColumn event.
Freeze a columnSet 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 sourceBind 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.
TitleDescription
Walkthrough: Display Data from a SQL Server Database in a DataGrid ControlDescribes 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 ControlDescribes how to create row details for a DataGrid.
How to: Implement Validation with the DataGrid ControlDescribes how to validate values in DataGrid cells and rows, and display validation feedback.
Default Keyboard and Mouse Behavior in the DataGrid ControlDescribes how to interact with the DataGrid control by using the keyboard and mouse.
How to: Group, Sort, and Filter Data in the DataGrid ControlDescribes how to view data in a DataGrid in different ways by grouping, sorting, and filtering the data.
Sizing Options in the DataGrid ControlDescribes how to control absolute and automatic sizing in the DataGrid.

DataGrid
Styling and Templating
Data Binding Overview
Data Templating Overview
Controls
WPF Content Model

Show: