Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
40
When grouping by a column the selected data row is changed to select a group by row.
posted

Hi,

I have a problem with group by. When I select 1 or more rows in the grid and then group by a column, my selection is LOST and the first group by row is selected instead. Is there any way to make the grid remember my selection and then expand the group by rows that contains my selected rows and then make them selected again?

 I have tried looking for event that could tell me when group by has occurred (e.g like BeforeGroupBy and AfterGroupBy). but they do not seem to exist. Are these events perhaps available in future versions, so that you can store the selected rows before group by and then re apply my selection after group by?

Michael

 

 

 

Parents
  • 469350
    Offline posted

    Hi Michael,

    When you group the grid, the existing row objects are destroyed and a completely new set of rows, including GroupByRows are created to display the new structure. So the grid cannot maintain the selection.

    What you would have to do is store some kind of key information from the data of the selected rows before the grouping takes place, and then search through the rows to find the new rows that represent the same data and re-select these after the grouping takes place.

    Grouping is tightly tied to sorting, so the events you are looking for are BeforeSortChange and AfterSortChange.

    Here's some sample code. In my grid, I have a primary key value in a column called "Key". So I am using the BeforeSortChange to check for changes in the grouping, and if there are any, I store the Key value of each selected row. Then in the AfterSortChange, I re-select all of those rows and make sure they are expanded.

    Note that this works fine in a small test project with 100 rows. But if you have a very large number of rows in your grid, the loop I am doing in the AfterSortChange event could take some time and may not be practical.


            List<int> selectedRowKeys = new List<int>();
            private void ultraGrid1_BeforeSortChange(object sender, BeforeSortChangeEventArgs e)
            {           
                // Check to see if the Grouping has changed on any column.
                bool groupingHasChanged = false;
                foreach (UltraGridColumn column in e.SortedColumns)
                {
                    UltraGridColumn originalColumn = column.Band.Columns[column.Key];

                    // Compare the IsGroupByColumn on the new sorted columns to the value
                    // of the original column to see if it is different.
                    if (column.IsGroupByColumn != originalColumn.IsGroupByColumn)
                    {
                        groupingHasChanged = true;
                        break;               
                    }               
                }

                if (groupingHasChanged)
                {
                    // If the grouping has changed, then store the primary key value of every
                    // selected row.
                    UltraGrid grid = (UltraGrid)sender;
                    foreach (UltraGridRow row in grid.Selected.Rows)
                    {
                        selectedRowKeys.Add((int)row.Cells["Key"].Value);
                    }            
                }
            }

            private void ultraGrid1_AfterSortChange(object sender, BandEventArgs e)
            {
                UltraGrid grid = (UltraGrid)sender;           
                           
                if (selectedRowKeys != null && selectedRowKeys.Count > 0)
                {
                    // Loop through every data row (non-GroupByRow) in the grid.
                    foreach (UltraGridRow row in grid.Rows.GetFilteredInNonGroupByRows())
                    {
                        // Get the primary key value of the row.
                        int key = (int)row.Cells["Key"].Value;

                        // If the key exists in the selectedRowKeys, then select the row
                        // and make sure all of it's parent rows all the way up the chain are
                        // expanded.
                        if (selectedRowKeys.Contains(key))
                        {
                            // Remove this key from the list, since we already found it.
                            // This will make subsequent searches more efficient.
                            selectedRowKeys.Remove(key);

                            // Expand the parent rows of this row all the way up the chain.
                            this.ExpandAllParents(row);

                            // Select the row.
                            row.Selected = true;
                        }

                        // If the selectedRowKeys list is empty, then we are done and we
                        // can stop looping. This is not strictly neccessay, but it's
                        // efficient.
                        if (selectedRowKeys.Count == 0)
                            break;
                    }
                }

                // Clear out the collection.
                selectedRowKeys.Clear();
            }

            private void ExpandAllParents(UltraGridRow row)
            {
                // Get the parent row.
                UltraGridRow parentRow = row.ParentRow;
                while (parentRow != null)
                {               
                    // Expand the parent row
                    parentRow.Expanded = true;

                    // Walk up the chain to the parent of the parent.
                    parentRow = parentRow.ParentRow;               
                }
            }

Reply Children
No Data