Table of Contents

Rows

Data rows represent items from the bound item source. When you group data, Data Grid creates group rows to combine rows with identical group values. Group rows do not exist in the item source.

datagrid-data-and-group-rows

Row Height

All grid rows initially have the same row height, required to display a single line of text. Data Grid allows you to set a custom row height, as well as enable automatic row height calculation for displaying large text data in cells.

Custom Row Height

Use the DataControlBase.RowMinHeight property to set the minimum row height.

If the automatic row height feature is disabled, all rows have the same height specified by the DataControlBase.RowMinHeight property.

Row Auto-Height

If cells in a specific column contain large text data, you can enable text wrapping for this column, allowing rows to adjust their height to fully display the cell contents.

datagrid-rowautoheight

To enable text wrapping for column cells, assign a TextEditorProperties object (or its descendant; for example, ButtonEditorProperties) to the GridColumn.EditorProperties property, and set the TextEditorProperties.TextWrapping option to Wrap.

Tip

A TextEditorProperties object is used to assign a TextEditor in-place editor to a grid column. The TextEditorProperties object stores settings specific to a TextEditor. When you activate a cell editor at runtime, a real in-place editor is created from the specified TextEditorProperties object. See Data Editing for more information.

The following code enables text wrapping for a grid column.

<mxdg:GridColumn FieldName="Notes" Width="80" MinWidth="80">
    <mxdg:GridColumn.EditorProperties>
        <mxe:TextEditorProperties TextWrapping="Wrap"/>
    </mxdg:GridColumn.EditorProperties>
</mxdg:GridColumn>

Identify and Get Rows

To allow row identification, Data Grid assigns row indexes to data and group rows. Row indexes identify rows in API members used to retrieve and set cell values, move focus between rows, and iterate through rows.

  • Row indexes reflect the order of data and group rows in the Data Grid.
  • Data rows are numbered using zero-based non-negative row indexes. The top data row has a row index of 0, the second data row has a row index of 1, and so on.
  • Group rows are numbered using negative row indexes. The top group row has a row index of -1, the second group row has a row index of -2, and so on
  • Row indexes are used to identify both visible and hidden (within collapsed groups) rows.
  • When the order of rows changes (for example, when data is sorted or grouped), rows are given new row indexes according to their new positions.
  • Row indexes are not assigned to rows that are hidden due to data filtering.

When data is not grouped, row indexes match visible row indexes:

datagrid-rowindexes

When data is grouped, row indexes and visible row indexes do not match:

datagrid-rowindexes-grouping

Special Row Indexes

Data Grid reserves the following predefined row indexes to identify special rows:

  • DataControlBase.AutoFilterRowIndex constant — Identifies the Auto Filter Row. This row allows a user to type text in its cells to filter data against corresponding columns. See the following topic for more information: Search and Filtering.
  • DataGridControl.InvalidRowIndex constant — Identifies a row that does not exist in the Data Grid control. This constant may be returned by DataGrid methods used to obtain row indexes.

Source Items and Source Item Indexes

Data rows correspond to items (business objects) in the bound item source (DataControlBase.ItemsSource). An item's position in the item source is called source item index.

You can use the following methods to obtain a data row's underlying source item and source item index.

  • GetSourceItemByRowIndex — Returns the source item (business object) of the row specified by its row index.
  • GetSourceItemByVisibleRowIndex — Returns the source item (business object) of the row specified by its visible index.
  • GetSourceItemIndexByRowIndex — Returns the source item index (index of the business object in the item source) of the row specified by its row index.
  • GetSourceItemIndexByVisibleRowIndex — Returns the source item index (index of the business object in the item source) of the row specified by its visible index.

To perform the opposite conversion of indexes, see the following methods:

  • GetRowIndexBySourceItemIndex — Returns the row index of the row specified by its source item index.
  • GetVisibleRowIndexBySourceItemIndex — Returns the visible index of the row specified by its source item index (index of the business object in the item source).

Source item indexes are zero-based. When you sort, group or filter rows, source item indexes of the grid rows do not change.

Group rows do not have corresponding items in the item source, so they cannot be addressed using source items and source item indexes.

Data Grid provides API members to retrieve rows by indexes, and to convert between row indexes, visible row indexes and indexes of data source items. The following list summarizes these API members:

  • FocusedRowIndex — Gets or sets the index of the focused row. You can use this property to move focus to a specific row.
  • GetRowIndexBySourceItemIndex — Returns the row index of the row specified by its source item index.
  • GetRowIndexByVisibleRowIndex — Returns the row index of the row specified by its visible index.
  • GetSourceItemByRowIndex — Returns the source item (business object) of the row specified by its row index.
  • GetSourceItemByVisibleRowIndex — Returns the source item (business object) of the row specified by its visible index.
  • GetSourceItemIndexByRowIndex — Returns the source item index (index of the business object in the item source) of the row specified by its row index.
  • GetSourceItemIndexByVisibleRowIndex — Returns the source item index (index of the business object in the item source) of the row specified by its visible index.
  • GetVisibleRowIndexByRowIndex — Returns the visible index of the row specified by its row index.
  • GetVisibleRowIndexBySourceItemIndex — Returns the visible index of the row specified by its source item index (index of the business object in the item source).
  • VisibleRowCount

Methods to iterate through group rows and their children:

Methods to obtain and set cell values:

Focused Row

Use the DataGridControl.FocusedRowIndex property to retrieve the focused row's index. The DataGridControl.FocusedItem property allows you to retrieve the focused row's underlying data object.

To move focus to a specific row, you can assign this row's index to the DataGridControl.FocusedRowIndex property.

Multiple Row Selection (Highlight)

Data Grid supports multiple row selection mode, which allows you and your user to select (highlight) multiple rows at one time.

grid-multipleselection

Set the SelectionMode property to Multiple to enable multiple row selection mode.

Select Rows Using the Mouse and Keyboard

Users can select multiple rows with the mouse and keyboard. They need to click rows while holding the CTRL and/or SHIFT key down.

Work with Row Selection in Code

The following API allows you to select/deselect rows, and identify whether a row is selected:

  • SelectAll
  • SelectRow
  • SelectRange
  • UnselectRow
  • ClearSelection
  • IsRowSelected

To retrieve the row selection, use the following members:

  • GetSelectedRowIndexes — Returns a collection of indexes of currently selected rows.
  • SelectedItems — Specifies the collection of data (business) objects that correspond to selected rows.

Handle the SelectionChanged event to respond to changes to the row selection.

A call to any method that changes a row's selected state causes an update of the DataGrid control, and raises the SelectionChanged event.

To perform batch modifications to the row selection and prevent superfluous updates, you can wrap the code that modifies rows' selected states with the BeginSelection and EndSelection method pair. In this case, the control redraws the selection, and the SelectionChanged event fires after the call to the EndSelection method.

dataGrid1.SelectionMode = Eremex.AvaloniaUI.Controls.DataControl.RowSelectionMode.Multiple;
// Start a batch update of the row selection.
dataGrid1.BeginSelection();
dataGrid1.ClearSelection();
dataGrid1.SelectRow(row1);
dataGrid1.SelectRow(row2);
//...
// Finish the batch update.
dataGrid1.EndSelection();

Focused Row vs Selected Rows

The focused row is the row that receives user input. The focused row may not be in sync with the selected (highlighted) row in multiple selection mode. See the following sections for mode details.

Focused Row in Single Selection Mode

In single selection mode, the focused row automatically gets the selected state. You can use the FocusedRowIndex property and GetSelectedRowIndexes method to retrieve the focused row.

Focused Row in Multiple Selection Mode

The focused and selected states are different in multiple row selection mode. Whether a row is selected or not, can be determined by the row's highlight. Only selected rows are highlighted.

In multiple selection mode, a click on a row focuses and selects this row at the same time. A user, however, can toggle the focused row's selected state using the following actions:

  • Press CTRL+SPACE.
  • Click the focused row while holding the CTRL key down.

When you select a row in code, this row does not get the focused state in multiple selection mode, and vice versa.

Get and Set Row Values

Data Grid provides the following methods to retrieve and set values in row cells:

  • DataGridControl.GetCellValue — Returns a value in a specific cell, addressed by a row and column (or field name).
  • DataGridControl.SetCellValue — Sets a value in a specific cell.

The following code retrieves a value from the focused row for the column bound to the FirstName field:

string firstName = dataGrid.GetCellValue(dataGrid.FocusedRowIndex, "FirstName") as String;

You can also get and set a data row's value at the item source level. Obtain the row's source item (business object), and then use the item's properties/methods to get/set item values. Use the following API members to retrieve source items:

  • DataControlBase.FocusedItem - Allows you to retrieve the source item object of the currently focused row.
  • DataGridControl.GetSourceItemByRowIndex — Returns a source item object by a row's index.
  • DataGridControl.GetSourceItemByVisibleRowIndex — Returns a source item object by a row's visible index.

The following code sets the HiredDate property for the focused row's business object:

EmployeeInfo emp = dataGrid.FocusedItem as EmployeeInfo;
if (employee != null )
{
    employee.HiredDate = DateTime.Today;
}