public bool SelectItemCells(
            int itemIndex,
            object item,
            HashedLinkedList <ColumnBase> columnsByVisiblePosition,
            bool preserveSelection)
        {
            bool selectionDone = true;
            int  columnsCount  = columnsByVisiblePosition.Count;

            m_toDeferSelect.Clear();

            SelectionCellRangeWithItems rangeWithItemsToSelect =
                new SelectionCellRangeWithItems(
                    new SelectionRange(itemIndex),
                    new object[] { item },
                    new SelectionRange(0, columnsCount - 1));

            SelectionCellRange cellRange = rangeWithItemsToSelect.CellRange;

            m_cellsToSelect.Clear();
            SelectedCellsStorage selectedCellsInChange = m_owner.SelectedCellsStore;

            // Remove all currently selected Cells from the new selectionRange
            // to avoid duplicate SelectionCellRange
            SelectedCellsStorage tempStorage = new SelectedCellsStorage(null);

            tempStorage.Add(rangeWithItemsToSelect);

            for (int i = 0; i < selectedCellsInChange.Count; i++)
            {
                tempStorage.Remove(selectedCellsInChange[i]);
            }

            foreach (ColumnBase column in columnsByVisiblePosition)
            {
                if (!column.Visible)
                {
                    tempStorage.Remove(new SelectionCellRangeWithItems(itemIndex, item, column.VisiblePosition));
                }
            }

            int tempStorageCount = tempStorage.Count;

            // All Cells are already selected
            if (tempStorageCount == 0)
            {
                if (!m_cellsToUnselect.Contains(cellRange))
                {
                    selectionDone = false;
                }

                m_cellsToUnselect.Clear();

                if (!preserveSelection)
                {
                    foreach (SelectionCellRangeWithItems selectedCellRangeWithItems in selectedCellsInChange)
                    {
                        m_cellsToUnselect.Add(selectedCellRangeWithItems);
                    }
                }

                m_cellsToUnselect.Remove(rangeWithItemsToSelect);
            }
            else
            {
                // Add each range to selection
                for (int i = 0; i < tempStorageCount; i++)
                {
                    m_cellsToSelect.Add(tempStorage[i]);
                }

                m_cellsToUnselect.Clear();

                if (!preserveSelection)
                {
                    foreach (SelectionCellRangeWithItems selectedCellRangeWithItems in selectedCellsInChange)
                    {
                        tempStorage = new SelectedCellsStorage(null);
                        tempStorage.Add(selectedCellRangeWithItems);
                        tempStorage.Remove(rangeWithItemsToSelect);
                        tempStorageCount = tempStorage.Count;

                        for (int i = 0; i < tempStorageCount; i++)
                        {
                            m_cellsToUnselect.Add(tempStorage[i]);
                        }
                    }
                }
            }

            if (!preserveSelection)
            {
                this.UnselectAllItems();
            }

            return(selectionDone);
        }
        public void Visit(DataGridContext sourceContext, ref bool stopVisit)
        {
            object[]       items;
            CollectionView itemsCollection = sourceContext.Items;
            int            count           = itemsCollection.Count;

            if (count == 0)
            {
                return;
            }

            if (sourceContext.ItemsSourceCollection is DataGridVirtualizingCollectionViewBase)
            {
                items = null;
            }
            else
            {
                items = new object[count];

                for (int i = 0; i < count; i++)
                {
                    items[i] = itemsCollection.GetItemAt(i);
                }
            }

            SelectionRange itemRange = new SelectionRange(0, count - 1);

            if (sourceContext.DataGridControl.SelectionUnit == SelectionUnit.Row)
            {
                sourceContext.DataGridControl.SelectionChangerManager.SelectItems(
                    sourceContext,
                    new SelectionRangeWithItems(itemRange, items));
            }
            else
            {
                HashedLinkedList <ColumnBase> columnsByVisiblePosition = sourceContext.ColumnsByVisiblePosition;
                SelectedItemsStorage          selectedColumnStore      = new SelectedItemsStorage(null);
                SelectionRange fullColumnRange = new SelectionRange(0, columnsByVisiblePosition.Count - 1);
                selectedColumnStore.Add(new SelectionRangeWithItems(fullColumnRange, null));
                int index = 0;

                foreach (ColumnBase column in columnsByVisiblePosition)
                {
                    if (!column.Visible)
                    {
                        selectedColumnStore.Remove(new SelectionRangeWithItems(new SelectionRange(index), null));
                    }

                    index++;
                }

                int columnRangeCount = selectedColumnStore.Count;

                for (int i = 0; i < columnRangeCount; i++)
                {
                    sourceContext.DataGridControl.SelectionChangerManager.SelectCells(
                        sourceContext,
                        new SelectionCellRangeWithItems(itemRange, items, selectedColumnStore[i].Range));
                }
            }
        }