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.
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.
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 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:
When data is grouped, row indexes and visible row indexes do not match:
Special Row Indexes
Data Grid reserves predefined row indexes to identify the following special rows:
Auto Filter Row (row index: DataControlBase.AutoFilterRowIndex
constant) — 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.
Invalid Row (row index: DataGridControl.InvalidRowIndex
constant) — Identifies a row that does not exist in the Data Grid control.
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
GetSourceItemByVisibleRowIndex
GetSourceItemIndexByRowIndex
GetSourceItemIndexByVisibleRowIndex
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.
Related API
Data Grid provides the 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 this information:
FocusedRowIndex
GetRowIndexBySourceItemIndex
GetRowIndexByVisibleRowIndex
GetSourceItemByRowIndex
GetSourceItemByVisibleRowIndex
GetSourceItemIndexByRowIndex
GetSourceItemIndexByVisibleRowIndex
GetVisibleRowIndexByRowIndex
GetVisibleRowIndexBySourceItemIndex
VisibleRowCount
Methods to traverse through group rows and their children:
GetGroupChildRowCount
GetGroupChildRowIndex
GetParentRowIndex
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.
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.DataGridControl.SetCellValue
— Sets a value in a specific cell.
The following code retrieves a value of the FirstName column from the focused row:
string firstName = dataGrid.GetCellValue(dataGrid.FocusedRowIndex, dataGrid.Columns["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 for the currently focused row.DataGridControl.GetSourceItemByRowIndex
— Returns a source item by a row's index.DataGridControl.GetSourceItemByVisibleRowIndex
— Returns a source item 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;
}