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
45
Manual synchronization of header checkbox with filtering and group by functionality
posted

Hi,

I'm trying to manually handle the synchronization of the header checkbox and the cell values in a grid with filtering and group by functionality enabled.

Filtered out rows should not be updated by the header checkbox, so in the AfterHeaderCheckStateChanged handler, I'm only updating the rows returned by the e.Rows.GetFilteredInNonGroupByRows() method:

 

 

 

        private void AfterHeaderCheckStateChanged_EventHandler(object sender, AfterHeaderCheckStateChangedEventArgs e)

        {

            if (_isInCellChange > 0) return; //Return if event is triggered by the SetHeaderCheckedState() in the CellChange event

 

            bool isChecked = e.Column.GetHeaderCheckedState(e.Rows) == CheckState.Checked;

            foreach (var filteredInNonGroupByRow in e.Rows.GetFilteredInNonGroupByRows())

            {

                filteredInNonGroupByRow.Cells[e.Column].SetValue(isChecked, false);

            }

        }

 

Updating the header checkbox when a cell value is changed is handled in the CellChange event:

 

        private void CellChange_EventHandler(object sender, CellEventArgs e)

        {

            if (e.Cell.Column.Index != 2) return; //Checkbox column is index 2

 

            _isInCellChange++;

 

            var cell = e.Cell;

            var column = cell.Column;

            var rowsCollection = cell.Row.ParentCollection;

            var newCellValue = (bool)cell.EditorResolved.Value;

 

            CheckState currentCheckState = column.GetHeaderCheckedState(rowsCollection);

 

            if (newCellValue)

            {

                if (currentCheckState != CheckState.Checked)

                {

                    bool shouldCheckHeader = true;

                    foreach (var row in rowsCollection.GetFilteredInNonGroupByRows())

                    {

                        var cellInRow = row.Cells[column];

                        if ((bool)cellInRow.EditorResolved.Value == false)

                        {

                            shouldCheckHeader = false;

                            break;

                        }

                    }

                    if (shouldCheckHeader)

                    {

                        column.SetHeaderCheckedState(rowsCollection, true);

                    }

                }

            }

            else

            {

                column.SetHeaderCheckedState(rowsCollection, false);

            }

            _isInCellChange--;

        }

 

 

Two issues:

 

When the grid is grouped by a column (not the checkbox column), the checkbox header is unchecked and all child-elements are set to false (unchecked). How can I prevent this?

When the filter is changed, the header checkbox is not updated - this is why the synchronization has to be done manually as far as I understand. But how can I force an update of the checkbox state after the filter is changed? Checkbox state should be determined based on the filtered in rows only.

 

Thanks in advance

 

Parents Reply Children
No Data