A data bound list control that displays the items from data source in a table. The DataGrid control allows you to select, sort, and edit these items. A Visual Studio Web site project with source code is available to accompany this topic: Download. The following code example demonstrates how to use the DataGrid control to display the items in the data source.

The following code example demonstrates how to use the DataGrid control for a simple shopping cart. Before you use the DataGrid control, consider using the GridView control instead. The GridView control, which was introduced in the. NET Framework version 2. Use the DataGrid control to display the fields of a data source as columns in a table.

Each row in the DataGrid control represents a record in the data source. The DataGrid control supports selection, editing, deleting, paging, and sorting. This control can be used to display user input, which might include malicious client script. Check any information that is sent from a client for executable script, SQL statements, or other code before displaying it in your application.

Validation server controls are also provided to assess user input. For more information, see Validation Server Control Syntax.

Different column types determine the behavior of the columns in the control. The following table lists the different column types that can be used. By default, the AutoGenerateColumns property is set to truewhich creates a BoundColumn object for each field in the data source. Each field is then rendered as a column in the DataGrid control in the order that each field appears in the data source. The columns specified are added to the Columns collection in the order listed. This allows you to programmatically control the columns in the DataGrid control.

The order that the columns are displayed in the DataGrid control is controlled by the order that the columns appear in the Columns collection. Although you can programmatically change the order of the columns by manipulating the Columns collection, it is easier to list the columns in the desired display order. Explicitly declared columns can be displayed in conjunction with automatically generated columns. When using both, explicitly declared columns will be rendered first, followed by the automatically generated columns.

Automatically generated columns are not added to the Columns collection. The appearance of the DataGrid control may be customized by setting the style properties for the different parts of the control. The following table lists the different style properties. You can also show or hide different parts of the control.

The following table lists the properties that control which parts are shown or hidden. The Control. Controls collection for the Item property of the DataGridItemEventArgs object passed into the event handler can be used to get the desired TableCell object.The DataGrid control provides a flexible way to display a collection of data in rows and columns.

The built-in column types include a text column, a check box column, a combobox column and a template column for hosting custom content. The built-in row type includes a drop-down details section that you can use to display additional content below the cell values. The DataGrid control supports common table formatting options, such as alternating row backgrounds and foregrounds and the ability to show or hide headers, grid lines, and scroll bars.

Additionally, the control provides several style and template properties that you can use to completely change the appearance of the control and its rows, columns, cells, and row or column headers. The DataGrid has built-in support for Narrator and Touch Narrator to help blind users to navigate and interact easily with the control.

Additionally, the control has built-in support for extensive keyboard navigation through cells, headers, rows and columns. The following guidance sections describe the additional concepts and techniques that you can use to build DataGrid control features into your applications:.

Here's the DataGrid sample source code. You may also leave feedback directly on GitHub. Skip to main content. Exit focus mode. Try it in the sample app. Is this page helpful? Yes No. Any additional feedback? Skip Submit. Send feedback about This product This page. This page. Submit feedback.

XAML - DataGrid

There are no open issues. View on GitHub.For more information, see Binding Sources Overview. Columns can display text, controls, such as a ComboBoxor 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. DataGrid can be customized in appearance, such as cell font, color, and size. DataGrid supports all styling and templating functionality of other WPF controls.

WPF DataGrid Control

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. Skip to main content. Exit focus mode. 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.

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.

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. Yes No. Any additional feedback? Skip Submit. Is this page helpful? Set the SelectionMode and SelectionUnit properties. Check the SelectedCells property to get the selected cells and the SelectedItems property to get the selected rows. Handle the AutoGeneratingColumn event. Set the FrozenColumnCount property to 1 and move the column to the left-most position by setting the DisplayIndex property to 0.

Describes how to create row details for a DataGrid.NET 4. The DataGrid provides a feature called AutoGenerateColumns that automatically generates column according to the public properties of your data objects. It generates the following types of columns: TextBox columns for string values CheckBox columns for boolean values ComboBox columns for enumerable values Hyperlink columns for Uri values.

Alternatively you can define your columns manually by setting the AutoGenerateColumns property to False. In this case you have to define the columns in the Columns collection of the data grid.

The data grid includes a variety of selection modes. They are configured by the SelectionMode and SelectionUnit property. The data grid provides features to sort, reorder and resize columns. The data grid also supports grouping. To enable grouping you have to define a CollectionView that contains to least one GroupDescription that defines the criterias how to group.

Second thing you need to do is defining a template how the groups should look like. You can do this by setting the GroupStyle to something like the following snippet.

The data grid provides a feature that shows a detail panel for a selected row. The data template gets the object that is bound to this row passed by the DataContext and can bind to it. You can specify a RowDetailsTemplateSelector that selects a data template according to the type or data that this row contains. To do this, create a type that derives from DataTemplateSelector and override the SelectTemplate method. In the items argument you get the data and you can determine which data template to display.

Return an instance of that data template as return value. You can define a an AlternatingRowBackground that is applied every even row. You can additionally specify an AlternationCount if you only want to ink every every n-th data row.The following example shows how to bind a DataGrid to a DataTable and use column auto-generation. The following example shows how to create a DataGrid with a customized Columns collection.

The DataGrid control provides a flexible way to display a collection of data in rows and columns. The DataGrid includes built-in column types and a template column for hosting custom content.

The built-in row type includes a drop-down details section that you can use to display additional content below the cell values. Each row in the data grid is bound to an object in the data source, and each column in the data grid is bound to a property of the data object.

To automatically reflect property changes, the objects in the source collection must implement the INotifyPropertyChanged interface. By default, the DataGrid control generates columns automatically when you set the ItemsSource property.

The type of column that is generated depends on the type of data in the column. The following table lists the generated columns types. When columns are auto-generated, you can handle the AutoGeneratingColumn event to customize or cancel columns before they are added to the DataGrid. If you add both user-defined columns and auto-generated columns to the DataGridthe user-defined columns are added first.

To rearrange the display order of the columns, you can set the DisplayIndex property for individual columns. You can prevent automatic column generation by setting the AutoGenerateColumns property to false.

This is useful if you want to create and configure all columns explicitly. Use the DataGridTemplateColumn type to define a custom column if the built-in column types do not meet your needs. The DataGridTemplateColumn type provides CellTemplate and CellEditingTemplate properties that enable you to specify content templates for both display and editing modes.

For example, you could define a custom column for dates. You can use the Columns collection to programmatically add, insert, remove, and change any columns in the control at run time.

datagrid

Check the IsAutoGenerated property to determine whether a column is auto generated or user defined. Auto-generated columns will be automatically added, removed, or regenerated when the ItemsSource changes. By default, the entire row is selected when a user clicks a cell in a DataGridand a user can select multiple rows.

You can set the SelectionUnit property to specify whether a user can select cells, full rows, or both. Set the SelectionMode property to specify whether multiple rows or cells can be selected, or only single rows or cells. You can get information about the cells that are selected from the SelectedCells property. By default, you can sort items in a DataGrid by clicking the column header.

You can customize sorting by handling the Sorting event. To cancel the default sort, set the Handled property to true. You can also sort the source data before it is displayed in the DataGrid. To group, sort, and filter data in the DataGridyou bind the DataGrid to an ICollectionView implementation that supports these operations.

You then perform the operations on the collection view. When items are grouped in the DataGridyou can define a GroupStyle that specifies the appearance of each group. If you have multiple levels of grouping, you can apply different styles to each group level.To use the DataGrid control in your Windows 10 application, you need to add the appropriate reference to your application as shown in the following section.

The toolkit is available as NuGet packages that can be added to any existing or new project using Visual Studio. Important : Build or higher is supported by current version of the Toolkit. Search for Microsoft. DataGridand choose the Microsoft. DataGrid nuget package.

datagrid

Alternatively, you can add the DataGrid control directly in C in your page code behind. You can use the DataGrid. ItemsSource property to bind to a collection that will be used to generate the contents of the DataGrid control. The following example demonstrates how to use the ItemsSource and AutoGenerateColumns properties to automatically display a collection of Customer data in rows and columns.

By default, the DataGrid control generates columns automatically when you set the ItemsSource property as shown above. If a property does not have a String or numeric value type, the generated text box columns are read-only and display the data object's ToString value.

You can override the default automatic generation of columns by setting the AutoGenerateColumns property to False and explicitly creating the bound columns with styling in XAML as shown below. In display mode the combobox column looks like the normal text column.

In edit mode the column allows the user to select a value from a provided collection displayed in a ComboBox control. ItemsSource IEnumerable : This property is bound to a collection of objects representing the elements of the combobox the source is the same for all cells in the column.

The ItemsSource elements of the column must have at least one property with the same name as the elements of the ItemsSource belonging to the DataGrid itself. It also links this property to a property with the same name within the column's ItemsSource. DisplayMemberPath string : This property tells the column which property value to show in both display and edit mode.

It's value must match the name of a property within the column's ItemsSource elements. The DataGridTemplateColumn type enables you to create your own column types by specifying the cell templates used to display values and enable editing. Set the CellTemplate property to specify the contents of cells that display values, but do not allow editing. Set the CellEditingTemplate property to specify the contents of cells in editing mode.

You may also leave feedback directly on GitHub. Skip to main content. Exit focus mode. Getting started with the DataGrid control The toolkit is available as NuGet packages that can be added to any existing or new project using Visual Studio. Add the namespaces to the toolkit using Microsoft. The DataGridComboBoxColumn contains the following specific properties: ItemsSource IEnumerable : This property is bound to a collection of objects representing the elements of the combobox the source is the same for all cells in the column.

WinForms DataGrid: Grouping, Filtering, Sorting, and Summaries

Yes No. Any additional feedback? Skip Submit. Send feedback about This product This page. This page. Submit feedback. There are no open issues.

datagrid

View on GitHub. Is this page helpful?A DataGrid represents a control that displays data in a customizable grid. It provides a flexible way to display a collection of data in rows and columns. Gets or sets a value that indicates whether the user can change the column display order by dragging column headers with the mouse.

Gets or sets a value that indicates whether the user can adjust the width of columns by using the mouse. Gets or sets a value that indicates whether the user can adjust the height of rows by using the mouse. Gets or sets a value that indicates whether the user can sort columns by clicking the column header.

When overridden in a derived class, is invoked whenever application code or internal processes call ApplyTemplate. Overrides FrameworkElement.

Sets the value of the DetailsVisibility property for the DataGridRow that contains the specified object. The following example shows how to display data in a DataGrid. Here is the XAML code to create two checkboxes with some properties and events. We recommend you to execute the above example code and experiment with some other properties and events. Previous Page. Next Page. Previous Page Print Page. AlternatingRowBackground Gets or sets the background brush for use on alternating rows.

AreRowDetailsFrozen Gets or sets a value that indicates whether the row details can scroll horizontally. AutoGenerateColumns Gets or sets a value that indicates whether the columns are created automatically. CanUserReorderColumns Gets or sets a value that indicates whether the user can change the column display order by dragging column headers with the mouse. CanUserResizeColumns Gets or sets a value that indicates whether the user can adjust the width of columns by using the mouse.

CanUserResizeRows Gets or sets a value that indicates whether the user can adjust the height of rows by using the mouse. CanUserSortColumns Gets or sets a value that indicates whether the user can sort columns by clicking the column header.

ColumnWidth Gets or sets the standard width and sizing mode of columns and headers in the DataGrid. CurrentItem Gets the data item bound to the row that contains the current cell. HorizontalScrollBarVisibility Gets or sets a value that indicates how horizontal scroll bars are displayed in the DataGrid. BeginEdit Invokes the BeginEdit command, which will place the current cell or row into edit mode. GenerateColumns Generates columns for the specified properties of an object. OnApplyTemplate When overridden in a derived class, is invoked whenever application code or internal processes call ApplyTemplate.

AutoGeneratedColumns Occurs when auto generation of all columns is completed. ColumnHeaderDragCompleted Occurs when the user releases a column header after dragging it by using the mouse.

ColumnHeaderDragDelta Occurs every time the mouse position changes while the user drags a column header. ColumnHeaderDragStarted Occurs when the user begins dragging a column header by using the mouse. ColumnReordered Occurs when a column moves to a new position in the display order. ColumnReordering Occurs before a column moves to a new position in the display order. CopyingRowClipboardContent Occurs after the default row content is prepared. LoadingRow Occurs after a DataGridRow is instantiated, so that you can customize it before it is used.