Пример #1
0
        /// <summary>
        /// Fired when calling SortRangeRows method
        /// </summary>
        /// <param name="e">The <see cref="Fr.Medit.MedDataGrid.SortRangeRowsEventArgs"/> instance containing the event data.</param>
        /// <remarks>
        /// This could be optimised by removing the duplicated sorting.
        /// </remarks>
        protected override void OnSortingRangeRows(SortRangeRowsEventArgs e)
        {
            base.OnSortingRangeRows(e);

              if (CustomSort == false && EnableSort == true)
              {
            if (e.AbsoluteColumnIndexes > e.Range.End.Column && e.AbsoluteColumnIndexes < e.Range.Start.Column)
            {
              throw new ArgumentOutOfRangeException("e", "Invalid e.AbsoluteColKeys");
            }

            Range range = e.Range;

            if (this.FixedRows > 1 && this.FixedRows > e.Range.Start.Row)
            {
              range = new Range(this.FixedRows - e.Range.Start.Row + 1, e.Range.Start.Column, e.Range.End.Row, e.Range.End.Column);
            }

            ICell[][] sortableRange = new ICell[range.End.Row - range.Start.Row + 1][];
            ICell[] cellsKeys = new ICell[range.End.Row - range.Start.Row + 1];
            ICell[] selectCellsKeys = new ICell[range.End.Row - range.Start.Row + 1];

            bool[][] sortableSelection = new bool[range.End.Row - range.Start.Row + 1][];
            List<Range> selectionRange = new List<Range>();

            int sortableRow = 0;

            for (int row = range.Start.Row; row <= range.End.Row; row++)
            {
              int sortableColumn = 0;

              sortableRange[sortableRow] = new ICell[range.End.Column - range.Start.Column + 1];
              cellsKeys[sortableRow] = this[row, e.AbsoluteColumnIndexes];
              selectCellsKeys[sortableRow] = this[row, e.AbsoluteColumnIndexes];
              sortableSelection[sortableRow] = new bool[range.End.Column - range.Start.Column + 1];
              for (int column = range.Start.Column; column <= range.End.Column; column++)
              {
            sortableRange[sortableRow][sortableColumn] = this[row, column];

            if (this.doKeepSelection == true && this[row, column] != null)
            {
              sortableSelection[sortableRow][sortableColumn] = this[row, column].Select;
            }

            sortableColumn++;
              }

              sortableRow++;
            }

            IComparer cellComparer = e.CellComparer;
            if (cellComparer == null)
            {
              cellComparer = new ValueCellComparer();
            }

            // SAA TODO: This is inefficient - we don't want 2 identical sorts!
            Array.Sort(cellsKeys, sortableRange, cellComparer);
            if (this.doKeepSelection == true)
            {
              Array.Sort(selectCellsKeys, sortableSelection, cellComparer);
            }

            // Apply sort
            sortableRow = 0;
            for (int i = 0; i <= range.End.Row - range.Start.Row; i++)
            {
              int row;
              if (e.Ascending)
              {
            row = range.Start.Row + i;
              }
              else
              {
            row = range.End.Row - i;
              }
              int sortableColumn = 0;
              for (int column = range.Start.Column; column <= range.End.Column; column++)
              {
            ICell sortedCell = sortableRange[sortableRow][sortableColumn];

            if (sortedCell != null && sortedCell.Grid != null && sortedCell.Range.Start.Row >= 0 && sortedCell.Range.Start.Column >= 0)
            {
              RemoveCell(sortedCell.Range.Start.Row, sortedCell.Range.Start.Column);
            }

            this[row, column] = sortedCell;
            sortableColumn++;
              }

              if (this.doKeepSelection == true && sortableSelection[sortableRow][1] == true)
              {
            Range ra = new Range(new Position(row, 0), new Position(row, range.End.Column));
            selectionRange.Add(ra);
              }
              sortableRow++;
            }

            if (this.doKeepSelection == true)
            {
              // and now we restore the selection in the grid
              foreach (Range selectedRange in selectionRange)
              {
            Selection.AddRange(selectedRange);
              }
            }
              }

              this.OnSortedRangeRows(e);
        }
Пример #2
0
        /// <summary>
        /// Fired when calling SortRangeRows method
        /// </summary>
        /// <param name="e">The <see cref="Fr.Medit.MedDataGrid.SortRangeRowsEventArgs"/> instance containing the event data.</param>
        /// <remarks>
        /// This could be optimised by removing the duplicated sorting.
        /// </remarks>
        protected override void OnSortingRangeRows(SortRangeRowsEventArgs e)
        {
            base.OnSortingRangeRows(e);

            if (CustomSort == false && EnableSort == true)
            {
                if (e.AbsoluteColumnIndexes > e.Range.End.Column && e.AbsoluteColumnIndexes < e.Range.Start.Column)
                {
                    throw new ArgumentOutOfRangeException("e", "Invalid e.AbsoluteColKeys");
                }

                Range range = e.Range;

                if (this.FixedRows > 1 && this.FixedRows > e.Range.Start.Row)
                {
                    range = new Range(this.FixedRows - e.Range.Start.Row + 1, e.Range.Start.Column, e.Range.End.Row, e.Range.End.Column);
                }

                ICell[][] sortableRange   = new ICell[range.End.Row - range.Start.Row + 1][];
                ICell[]   cellsKeys       = new ICell[range.End.Row - range.Start.Row + 1];
                ICell[]   selectCellsKeys = new ICell[range.End.Row - range.Start.Row + 1];

                bool[][]     sortableSelection = new bool[range.End.Row - range.Start.Row + 1][];
                List <Range> selectionRange    = new List <Range>();

                int sortableRow = 0;

                for (int row = range.Start.Row; row <= range.End.Row; row++)
                {
                    int sortableColumn = 0;

                    sortableRange[sortableRow]     = new ICell[range.End.Column - range.Start.Column + 1];
                    cellsKeys[sortableRow]         = this[row, e.AbsoluteColumnIndexes];
                    selectCellsKeys[sortableRow]   = this[row, e.AbsoluteColumnIndexes];
                    sortableSelection[sortableRow] = new bool[range.End.Column - range.Start.Column + 1];
                    for (int column = range.Start.Column; column <= range.End.Column; column++)
                    {
                        sortableRange[sortableRow][sortableColumn] = this[row, column];

                        if (this.doKeepSelection == true && this[row, column] != null)
                        {
                            sortableSelection[sortableRow][sortableColumn] = this[row, column].Select;
                        }

                        sortableColumn++;
                    }

                    sortableRow++;
                }

                IComparer cellComparer = e.CellComparer;
                if (cellComparer == null)
                {
                    cellComparer = new ValueCellComparer();
                }

                // SAA TODO: This is inefficient - we don't want 2 identical sorts!
                Array.Sort(cellsKeys, sortableRange, cellComparer);
                if (this.doKeepSelection == true)
                {
                    Array.Sort(selectCellsKeys, sortableSelection, cellComparer);
                }

                // Apply sort
                sortableRow = 0;
                for (int i = 0; i <= range.End.Row - range.Start.Row; i++)
                {
                    int row;
                    if (e.Ascending)
                    {
                        row = range.Start.Row + i;
                    }
                    else
                    {
                        row = range.End.Row - i;
                    }
                    int sortableColumn = 0;
                    for (int column = range.Start.Column; column <= range.End.Column; column++)
                    {
                        ICell sortedCell = sortableRange[sortableRow][sortableColumn];

                        if (sortedCell != null && sortedCell.Grid != null && sortedCell.Range.Start.Row >= 0 && sortedCell.Range.Start.Column >= 0)
                        {
                            RemoveCell(sortedCell.Range.Start.Row, sortedCell.Range.Start.Column);
                        }

                        this[row, column] = sortedCell;
                        sortableColumn++;
                    }

                    if (this.doKeepSelection == true && sortableSelection[sortableRow][1] == true)
                    {
                        Range ra = new Range(new Position(row, 0), new Position(row, range.End.Column));
                        selectionRange.Add(ra);
                    }
                    sortableRow++;
                }

                if (this.doKeepSelection == true)
                {
                    // and now we restore the selection in the grid
                    foreach (Range selectedRange in selectionRange)
                    {
                        Selection.AddRange(selectedRange);
                    }
                }
            }

            this.OnSortedRangeRows(e);
        }