Rows
- 7 minutes to read
- Row Height
- Auto Row Height
- Hide Horizontal Row Borders
- Row Indicator Panel
- Row Multi Select
- Web Style Row Selection
- Accessing Rows in Code. Row Handles
- Traversing Rows
- Data Editing: Add and Remove Rows
Row Height
There are two types of rows in the Data Grid: data rows (that represent data source records) and non-data service rows (group rows, new item rows, etc.).
By default, row height is determined by font settings. The Data Grid provides the following API to modify these heights.
-
Specifies the data row height in pixels. The default value is -1 and rows have no constant height.
-
Specifies the height of column headers.
-
Gets or sets the group rows’ height.
-
Specifies the vertical distance between rows. Setting this property to zero does not remove horizontal lines between rows. To do that, disable the GridOptionsView.ShowHorizontalLines setting.
-
This event repeatedly rises for every row and allows you to set individual height parameters for each of them. The code snippet below illustrates how to set row heights based on values from the “RowHeight” data source field.
Auto Row Height
By default, row cells clip content that they cannot display entirely. To change this behavior, utilize the MemoEdit, TokenEdit or PictureEdit editors as in-place editors for required columns and enable the GridOptionsView.RowAutoHeight setting. This will allow Data Grid data rows to dynamically adapt to the content and gain different heights.
Demo: Auto Row Height
Hide Horizontal Row Borders
You can hide column and row borders by disabling the GridOptionsView.ShowVerticalLines and GridOptionsView.ShowHorizontalLines settings.
Row Indicator Panel
A row indicator panel is a horizontal strip docked to the Data Grid’s left edge. End-users can click this bar to select any Data Grid row.
Row indicator panel displays various icons depending on what row is currently selected and which row operation is ongoing.
- the focused row.
- a row cell is being edited by an end-user.
- the row has been modified.
- the focused row is a new item row.
- the focused row is an auto-filter row.
- end-users can click this icon to maximize a detail View.
- end-users can click this icon to restore a detail View.
- the row contains errors.
- the focused row contains errors.
Related API
GridOptionsView.ShowIndicator - allows you to hide the row indicator panel.
GridView.IndicatorWidth - gets or sets the panel width.
GridView.CustomDrawRowIndicator - handle this event to manually redraw the panel.
Row Multi-Select
If the ColumnViewOptionsSelection.MultiSelect option is enabled, end-users are able to select multiple rows using marquee selection, keyboard arrow keys and mouse clicks with the Ctrl/Shift keys pressed.
Related API
ColumnView.SelectRow - Adds a row (card) to the current selection.
ColumnView.GetSelectedRows - Returns the handles of the selected rows or cards. Multiple rows can be selected when the ColumnViewOptionsSelection.MultiSelect option is set to true.
ColumnView.DeleteSelectedRows - Deletes the selected rows/cards in multiple selection mode or focused row/card in single selection mode.
Web Style Row Selection
Along with clicking a row indicator panel, end-users can utilize check boxes to select data rows. To enable these check boxes, set the GridOptionsSelection.MultiSelectMode property to the GridMultiSelectMode.CheckBoxRowSelect value. Web style selection is available only when row multi-select is on.
Related API
GridOptionsSelection.ShowCheckBoxSelectorInColumnHeader - enable this to display a check box in the column header area. Allows users to select all Data Grid rows at once.
GridOptionsSelection.ShowCheckBoxSelectorInGroupRow - enable this to display check boxes in group rows. Allows users to select all Data Grid rows that belong to a specific group.
Demo: Web Style Row Selection
Accessing Rows in Code. Row Handles
Every Data Grid row has three integer values that identify it: a data source index, a row handle and a visible index.
Data source indexes
- Specify zero-based row indexes in the bound list.
- Constant values that do not change when you sort, group or filter data.
- For group rows, they will point to the first data row in the group.
- Used for accessing data.
Row handles
- Zero-based indexes that correspond to row order from top to bottom.
- Group row handles are negative values that start with -1. The order matches the order of group rows from top to bottom.
- The grid specifies reserved row handles for the New Item Row, Auto Filter Row and an Invalid Row.
- Row handles are re-assigned to rows after each data operation.
- When the View is filtered, rows and row handles are created only for rows that match the filter.
Visible indexes
- Zero-based indexes that match the order of visible rows, from top to bottom.
- Service rows get negative indexes if displayed above data and group rows.
- Re-assigned after each data operation, including data sorting, grouping and filtering.
- Visible indexes are only assigned to rows in expanded groups. Thus, the indexes are updated after each expand/collapse operation.
For master-detail data, all detail Views have their own unique visible indexes and row handles.
Related API
ColumnView.FocusedRowHandle - returns the row handle of the currently focused row.
ColumnView.GetRowHandle, ColumnView.GetDataSourceRowIndex - return row handles by indexes of related records in a data source and vice versa.
ColumnView.GetVisibleIndex, ColumnView.GetVisibleRowHandle - return row handles by these rows’ visible indexes and vice versa.
ColumnView.GetRow - returns an object that represents a row with the given handle.
GridControl.NewItemRowHandle, GridControl.AutoFilterRowHandle - numeric constants that specify row handles for the new item and auto-filter rows respectively.
GridControl.InvalidRowHandle - a constant that is returned when obtaining a particular row fails. For example, an invalid row handle is returned by the GridView.GroupRowCollapsing event arguments if this event fires when all Data Grid groups collapse at once (e.g., after calling the GridView.CollapseAllGroups method).
GridView.TopRowIndex - scrolls a View up or down to a row with the required visible index.
Traversing Rows
When you need to process all Data Grid rows one-by-one, use the following approach.
- Read the BaseView.DataRowCount property value to determine the actual number of existing rows.
- To alter all existing rows, implement a loop that goes over row handles starting from 0 to DataRowCount - 1.
- If you need to process visible rows only, begin with a row that has a zero handle and then obtain the next rows by calling the ColumnView.GetNextVisibleRow method.
- Wrap your loop inside the ColumnView.BeginSort/ColumnView.EndSort methods pair to prevent reloading data while processing rows is still in progress. Otherwise, changing cell values may change a row order (together with row handles), which will cause your row processing algorithm to malfunction.
The code sample below iterates through grid records and reduces the “Price” column values by 10 percent.
private void UpdatePrice(DevExpress.XtraGrid.Views.Base.ColumnView View) {
// Obtain the Price column.
DevExpress.XtraGrid.Columns.GridColumn col = View.Columns.ColumnByFieldName("Price");
if (col == null) return;
View.BeginSort();
try {
// Obtain the number of data rows.
int dataRowCount = View.DataRowCount;
// Traverse data rows and change the Price field values.
for (int i = 0; i < dataRowCount; i++) {
object cellValue = View.GetRowCellValue(i, col);
double newValue = Convert.ToDouble(cellValue) * 0.9;
View.SetRowCellValue(i, col, newValue);
}
} finally { View.EndSort(); }
}