DataGrid

Multi-column data grid with typed columns, sorting indicators, and row selection.

Overview

DataGrid extends Selector and provides a tabular data view with typed columns, sortable headers, and row selection. Columns follow a WPF-aligned type hierarchy:

When a column is sorted, the header displays for ascending and for descending. Column separators use , header lines use , and crossings use .

Basic Example

<DataGrid ItemsSource="{Binding Employees}" SelectedIndex="0">
  <DataGrid.Columns>
    <DataGridTextColumn Header="Name" Binding="Name" />
    <DataGridTextColumn Header="Department" Binding="Department" />
    <DataGridCheckBoxColumn Header="Active" Binding="Active" Width="10" />
  </DataGrid.Columns>
</DataGrid>

Properties

PropertyTypeDefaultDescription
SelectedBackgroundColorBlueBackground color of the selected row.
SelectedForegroundColorWhiteForeground color of the selected row.
HeaderBackgroundColorDarkGrayBackground color of the column header row.
HeaderForegroundColorWhiteForeground color of the column header row.
ShowGridLinesbooltrueWhether to display column separators and header line using box-drawing characters.
CanSortbooltrueWhether sorting is enabled globally for the grid.
SelectedIndexint-1Zero-based index of the currently selected row. Inherited from Selector.
SelectedItemobjectnullThe currently selected data item. Inherited from Selector.
ItemsSourceIEnumerablenullData source for generating rows. Inherited from ItemsControl.

Column Types

DataGridColumn (abstract base)

PropertyTypeDefaultDescription
Headerstring""Text displayed in the column header.
Widthint0 (star)Character width. 0 = fill remaining space (star sizing).
SortDirectionSortDirectionNoneCurrent sort direction: None, Ascending, or Descending.
CanUserSortbooltrueWhether this column can be sorted.
SortMemberPathstring?nullProperty path for sorting. Bound columns default to their Binding path.

DataGridBoundColumn (abstract, extends DataGridColumn)

PropertyTypeDefaultDescription
Bindingstring?nullProperty path on each data item. Resolved via PropertyAccessorRegistry (AOT-safe).

DataGridTextColumn (extends DataGridBoundColumn)

Displays the bound value as text. Falls back to ToString() if Binding is not set.

DataGridCheckBoxColumn (extends DataGridBoundColumn)

Displays a boolean value as [x] (true) or [ ] (false), centered in the cell.

DataGridTemplateColumn (extends DataGridColumn)

PropertyTypeDefaultDescription
CellTemplateDataTemplate?nullTemplate used to render each cell. DataContext is set to the row's data item.

Examples

Text and CheckBox Columns

<DataGrid ItemsSource="{Binding Employees}" SelectedIndex="0">
  <DataGrid.Columns>
    <DataGridTextColumn Header="Id" Binding="Id" Width="6" />
    <DataGridTextColumn Header="Name" Binding="Name" />
    <DataGridTextColumn Header="Department" Binding="Department" />
    <DataGridCheckBoxColumn Header="Active" Binding="Active" Width="10" />
  </DataGrid.Columns>
</DataGrid>

Template Column

<DataGrid ItemsSource="{Binding Items}">
  <DataGrid.Columns>
    <DataGridTextColumn Header="Name" Binding="Name" />
    <DataGridTemplateColumn Header="Status">
      <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <TextBlock Text="{Binding Status}"
                     Foreground="Green" />
        </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
  </DataGrid.Columns>
</DataGrid>

Sorting Columns

<DataGrid CanSort="True">
  <DataGrid.Columns>
    <DataGridTextColumn Header="Name" Binding="Name"
                        SortDirection="Ascending" />
    <DataGridTextColumn Header="Score" Binding="Score"
                        CanUserSort="True" />
    <DataGridCheckBoxColumn Header="Passed" Binding="Passed"
                            CanUserSort="False" />
  </DataGrid.Columns>
</DataGrid>

Without Grid Lines

<DataGrid ShowGridLines="False" SelectedIndex="0">
  <DataGrid.Columns>
    <DataGridTextColumn Header="File" Binding="FileName" />
    <DataGridTextColumn Header="Size" Binding="Size" Width="10" />
  </DataGrid.Columns>
</DataGrid>

Keyboard Shortcuts

KeyAction
Up ArrowMove selection to the previous row.
Down ArrowMove selection to the next row.
HomeMove selection to the first row.
EndMove selection to the last row.

Key Concepts