Skip to main content

Traversing Rows

  • 5 minutes to read

This topic describes how to traverse (access one by one) through rows displayed within ASPxGridView. This includes:

  1. Rows Displayed Within the Current Page
  2. Visible Rows
  3. All Data Rows

Rows Displayed Within the Current Page

ASPxGridView provides two server-side events that fire for each row (data row, group row, etc.) before it is rendered onscreen, and enable you to customize its style, templates, or perform required calculations. These are:

  • ASPxGridView.HtmlRowCreated - this event is raised for each row, displayed within the current page, when the corresponding HTML table row has been created. You can handle this event, for example, to initialize web controls contained within grid templates.
  • ASPxGridView.HtmlRowPrepared - this event is raised for each row, displayed within the current page, when the corresponding HTML table row has been prepared but isn’t yet rendered. You can handle this event to change the style settings of individual rows.

Both events provide the same set of parameters that allow you to identify the currently processed row’s type, visible index, key value, etc. For an example, see How to: Dynamically Change Images Displayed Within Data Cells.

Important Note:

ASPxGridView provides the ASPxGridView.GetCurrentPageRowValues method, which returns row values displayed within the current page. The method’s parameter allows you to specify data source fields whose values should be returned.

In some instances, however, it is needed to traverse through current page rows one by one. If data is grouped, the current page can contain both data and group rows. To identify whether data grouping is applied, check the number of grouped columns.

bool dataGroupingApplied = grid.GetGroupedColumns().Count != 0;
ProcessRowsOnCurrentPage(dataGroupingApplied);

Data and group rows displayed within the grid can be identified by their visible indexes. To traverse through the current page rows, you need to obtain the starting and ending visible indices.

private void ProcessRowsOnCurrentPage(bool dataGroupingApplied) {
    // The visible index of the first row within the current page.
    int startVisibleIndex = grid.VisibleStartIndex;
    // The number of visible rows displayed within the current page.
    int visibleRowCount = grid.GetCurrentPageRowValues("ProductID").Count;
    // The visible index of the last row within the current page.
    int endVisibleIndex = startVisibleIndex + visibleRowCount - 1;

    if (dataGroupingApplied)
        ProcessDataAndGroupRows(startVisibleIndex, endVisibleIndex);
    else
        ProcessDataRows(startVisibleIndex, endVisibleIndex);
}

// Processes only data rows.
private void ProcessDataRows(int startIndex, int endIndex) {
    for (int i = startIndex; i <= endIndex; i++)
        ProcessDataRow(i);
}
// Processes data and group rows.
private void ProcessDataAndGroupRows(int startIndex, int endIndex) {
    for (int i = startIndex; i <= endIndex; i++) {
        if (IsGroupRow(i)) {
            // Do required actions on the processed group row.
            // ...
        }
        else
            ProcessDataRow(i);
    }
}
// Processes the specified data row.
private void ProcessDataRow(int visibleIndex) {
    DataRow row = grid.GetDataRow(visibleIndex);
    // Do required actions on the processed data row.
    // ...
}
// Indicates whether the specified row is a group row.
private bool IsGroupRow(int visibleIndex) {
    int rowLevel = grid.GetRowLevel(visibleIndex);
    return rowLevel != grid.GetGroupedColumns().Count;
}

Visible Rows

The number of visible rows is returned by the grid’s ASPxGridView.VisibleRowCount property. This includes both data rows and group rows (if data grouping is applied). Data and group rows contained within collapsed groups are not counted. End-users can see the number of visible rows within the grid’s Pager.

ASPxGridView_VisibleRowCount

The following code shows how to traverse through all visible rows one by one.

private void ProcessAllVisibleRows(bool dataGroupingApplied) {
    int startVisibleIndex = 0;
    int endVisibleIndex = grid.VisibleRowCount - 1;
    if (dataGroupingApplied)
        ProcessDataAndGroupRows(startVisibleIndex, endVisibleIndex);
    else
        ProcessDataRows(startVisibleIndex, endVisibleIndex);
}

All Data Rows

If ASPxGridView displayed all records from the bound data source (data isn’t grouped or filtered), you can process all data rows as shown in the Visible Rows section. Otherwise, use methods provided by the data source to access its records one by one.