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.v20.2.dll
NuGet Package:
DevExpress.Web
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.
|
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.
Example
The example demonstrates how to use custom grouping rules to group records by their first letters (the CustomColumnGroup event). The code in the BeforeColumnSortingGrouping event handler, is used to show an invisible column to demonstrate rows which are grouped.
View Example
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);
}
}
<dx:ASPxGridView ID="grid" runat="server" AutoGenerateColumns="False" DataSourceID="sds"
KeyFieldName="ProductID" OnCustomColumnGroup="grid_CustomColumnGroup"
OnCustomGroupDisplayText="grid_CustomGroupDisplayText" OnBeforeColumnSortingGrouping="grid_BeforeColumnSortingGrouping">
<Columns>
<dx:GridViewDataTextColumn FieldName="ProductID" ReadOnly="True" VisibleIndex="0">
<EditFormSettings Visible="False" />
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Name="VisibleProductName" FieldName="ProductName" VisibleIndex="1">
<Settings AllowGroup="True" SortMode="Custom" />
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Name="InvisibleProductName" FieldName="ProductName" VisibleIndex="2"
Visible="False">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="UnitPrice" VisibleIndex="2">
</dx:GridViewDataTextColumn>
</Columns>
<Settings ShowGroupPanel="True" />
</dx:ASPxGridView>
</div>
<asp:SqlDataSource ID="sds" runat="server" ConnectionString="<%$
ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice] FROM [Products]">
</asp:SqlDataSource>
<dx:ASPxGridView ID="grid" runat="server" AutoGenerateColumns="False" DataSourceID="sds"
KeyFieldName="ProductID" OnCustomColumnGroup="grid_CustomColumnGroup"
OnCustomGroupDisplayText="grid_CustomGroupDisplayText" OnBeforeColumnSortingGrouping="grid_BeforeColumnSortingGrouping">
<Columns>
<dx:GridViewDataTextColumn FieldName="ProductID" ReadOnly="True" VisibleIndex="0">
<EditFormSettings Visible="False" />
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Name="VisibleProductName" FieldName="ProductName" VisibleIndex="1">
<Settings AllowGroup="True" SortMode="Custom" />
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Name="InvisibleProductName" FieldName="ProductName" VisibleIndex="2"
Visible="False">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="UnitPrice" VisibleIndex="2">
</dx:GridViewDataTextColumn>
</Columns>
<Settings ShowGroupPanel="True" />
</dx:ASPxGridView>
</div>
<asp:SqlDataSource ID="sds" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice] FROM [Products]">
</asp:SqlDataSource>
Imports DevExpress.Web.ASPxGridView
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Function GetGroupNumber(ByVal str As String) As Integer
str = str.ToLower()
Dim ch As Char = str.Chars(0)
Dim i As Integer = Convert.ToInt32(ch)
If ch >= "a"c AndAlso ch <= "e"c Then
Return 1
End If
If ch >= "f"c AndAlso ch <= "j"c Then
Return 2
End If
If ch >= "k"c AndAlso ch <= "q"c Then
Return 3
End If
If ch >= "r"c AndAlso ch <= "v"c Then
Return 4
End If
If ch >= "x"c AndAlso ch <= "z"c Then
Return 5
End If
Return -1
End Function
Protected Sub grid_CustomColumnGroup(ByVal sender As Object, ByVal e As DevExpress.Web.ASPxGridView.CustomColumnSortEventArgs)
If e.Column.Name <> "VisibleProductName" Then
Return
End If
Dim res1 As Integer = GetGroupNumber(e.GetRow1Value(e.Column.FieldName).ToString())
Dim res2 As Integer = GetGroupNumber(e.GetRow2Value(e.Column.FieldName).ToString())
Dim res As Integer = res1 - res2
If res < 0 Then
res = 1
ElseIf res > 0 Then
res = -1
End If
e.Result = res
e.Handled = True
End Sub
Protected Sub grid_CustomGroupDisplayText(ByVal sender As Object, ByVal e As DevExpress.Web.ASPxGridView.ASPxGridViewColumnDisplayTextEventArgs)
Dim ind As Integer = GetGroupNumber(e.Value.ToString())
Select Case ind
Case 1
e.DisplayText = "A-E"
Case 2
e.DisplayText = "F-J"
Case 3
e.DisplayText = "K-Q"
Case 4
e.DisplayText = "R-V"
Case 5
e.DisplayText = "X-Z"
End Select
End Sub
Protected Sub grid_BeforeColumnSortingGrouping(ByVal sender As Object, ByVal e As DevExpress.Web.ASPxGridView.ASPxGridViewBeforeColumnGroupingSortingEventArgs)
If e.Column.Name = "VisibleProductName" Then
grid.Columns("InvisibleProductName").Visible = ((TryCast(grid.Columns("VisibleProductName"), GridViewDataColumn)).GroupIndex <> -1)
End If
End Sub
End Class
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the GroupIndex property.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
See Also