Tab
A newer version of this page is available. Switch to the current version.

GridViewDataColumn.GroupIndex Property

Gets or sets a value that specifies whether the column takes part in grouping and at which level.

Namespace: DevExpress.Web

Assembly: DevExpress.Web.v18.2.dll

Declaration

[DefaultValue(-1)]
public int GroupIndex { get; set; }
<DefaultValue(-1)>
Public Property GroupIndex As Integer

Property Value

Type Default Description
Int32

-1

An integer value that specifies the column's position among grouping columns. -1 if the ASPxGridView isn't grouped by the values of the current column.

Remarks

The ASPxGridView supports data grouping by an unlimited number of data columns. To group data by the values of a given column, set the column's GroupIndex property to a non-negative integer. If several columns are involved in grouping, the GroupIndex property specifies the grouping level. If set to 0, the column is at the root grouping level. Changing the GroupIndex property's value updates this property value for other columns involved in grouping.

To cancel grouping by the column's values, set the GroupIndex property to -1 or call the GridViewDataColumn.UnGroup method.

Data grouping is allowed if the ASPxGridViewBehaviorSettings.AllowGroup and ASPxGridBehaviorSettings.AllowSort properties are set to true.

Examples

The example demonstrates how to use custom grouping rules to group records by their first letters via the ASPxGridView.CustomColumnGroup event handler. The code in the ASPxGridView.BeforeColumnSortingGrouping event handler, is used to show an invisible column to demonstrate rows which are grouped.

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;

public partial class _Default : System.Web.UI.Page {
    protected int GetGroupNumber(string str) {
        str = str.ToLower();
        char ch = str[0];
        int i = Convert.ToInt32(ch);

        if (ch >= 'a' && ch <= 'e') return 1;
        if (ch >= 'f' && ch <= 'j') return 2;
        if (ch >= 'k' && ch <= 'q') return 3;
        if (ch >= 'r' && ch <= 'v') return 4;
        if (ch >= 'x' && ch <= 'z') return 5;

        return -1;
    }

    protected void grid_CustomColumnGroup(object sender, DevExpress.Web.ASPxGridView.CustomColumnSortEventArgs e) {
        if (e.Column.Name != "VisibleProductName") return;

        int res1 = GetGroupNumber(e.GetRow1Value(e.Column.FieldName).ToString());
        int res2 = GetGroupNumber(e.GetRow2Value(e.Column.FieldName).ToString());

        int res = res1 - res2;

        if (res < 0)
            res = 1;
        else if (res > 0)
            res = -1;

        e.Result = res;
        e.Handled = true;
    }

    protected void grid_CustomGroupDisplayText(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDisplayTextEventArgs e) {
        int ind = GetGroupNumber(e.Value.ToString());
        switch (ind) {
            case 1: e.DisplayText = "A-E"; break;
            case 2: e.DisplayText = "F-J"; break;
            case 3: e.DisplayText = "K-Q"; break;
            case 4: e.DisplayText = "R-V"; break;
            case 5: e.DisplayText = "X-Z"; break;
        }
    }

    protected void grid_BeforeColumnSortingGrouping(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewBeforeColumnGroupingSortingEventArgs e) {
        if (e.Column.Name == "VisibleProductName")
            grid.Columns["InvisibleProductName"].Visible = ((grid.Columns["VisibleProductName"] as GridViewDataColumn).GroupIndex != -1);
    }
}