Ejemplo n.º 1
0
        public static void RestoreSelection <TRow>(
            [NotNull] DataGridView grid,
            [NotNull] BoundDataGridSelectionState <TRow> selectionState,
            bool forceSelectedRowsVisible = true) where TRow : class
        {
            grid.ClearSelection();

            if (selectionState.SelectionCount == 0)
            {
                return;
            }

            foreach (DataGridViewRow row in grid.Rows)
            {
                var tableRow = row.DataBoundItem as TRow;

                if (tableRow != null && selectionState.IsSelected(tableRow))
                {
                    // Note: during sorting, rows have Visible == false in *some* situations
                    if (forceSelectedRowsVisible)
                    {
                        // Ensure that the previously selected row is again visible. Otherwise the row may be
                        // immediately unselected if there is a filter defined for it
                        // (if the grid is a FilterableDataGridView)
                        row.Visible = true;
                    }

                    row.Selected = true;
                }
            }
        }
Ejemplo n.º 2
0
        private void _dataGridView_SortingColumn(object sender, SortingColumnEventArgs e)
        {
            _msg.VerboseDebugFormat("BoundDataGridHandler._dataGridView_SortingColumn");

            // the user is sorting on a column - remember the selected rows
            _savedSelection = GetSelectionState();
        }
Ejemplo n.º 3
0
        public void RestoreSelection(
            [NotNull] BoundDataGridSelectionState <ROW> selectionState)
        {
            Assert.ArgumentNotNull(selectionState, nameof(selectionState));

            if (_msg.IsVerboseDebugEnabled)
            {
                _msg.VerboseDebugFormat("Restoring selection ({0} row(s))",
                                        selectionState.SelectionCount);
            }

            DataGridViewUtils.RestoreSelection(_dataGridView, selectionState);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Binds a data grid view to the given binding list of rows
        /// </summary>
        /// <param name="rows">The rows.</param>
        /// <param name="autoGenerateColumns">Indicates if columns should be dynamically added based on the bound type</param>
        /// <param name="sortStateOverride">Optional sort state to apply regardless of the current sort state of the grid.</param>
        /// <param name="defaultSortState">Optional default sort state, to be applied when there is no current or override sort state.</param>
        /// <param name="selectionStateOverride"></param>
        public bool BindTo([NotNull] IList <ROW> rows,
                           bool autoGenerateColumns = false,
                           DataGridViewSortState sortStateOverride = null,
                           DataGridViewSortState defaultSortState  = null,
                           BoundDataGridSelectionState <ROW> selectionStateOverride = null)
        {
            Assert.ArgumentNotNull(rows, nameof(rows));

            return(DataGridViewUtils.BindTo(_dataGridView, rows,
                                            autoGenerateColumns,
                                            sortStateOverride,
                                            defaultSortState,
                                            selectionStateOverride));
        }
Ejemplo n.º 5
0
        private void _dataGridView_Sorted(object sender, EventArgs e)
        {
            _msg.VerboseDebugFormat("BoundDataGridHandler._dataGridView_Sorted");

            // if there are selected rows to restore, do it now
            if (_savedSelection == null)
            {
                return;
            }

            RestoreSelection(_savedSelection);

            _savedSelection = null;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Binds a data grid view to the given list of rows
        /// </summary>
        /// <param name="grid">The data grid view to bind to the list</param>
        /// <param name="rows">The rows.</param>
        /// <param name="autoGenerateColumns">Indicates if columns should be dynamically added based on the bound type</param>
        /// <param name="sortStateOverride">Optional sort state to apply regardless of the current sort state of the grid.</param>
        /// <param name="defaultSortState">Optional default sort state, to be applied when there is no current or override sort state.</param>
        /// <param name="selectionStateOverride"></param>
        public static bool BindTo <TRow>(
            [NotNull] DataGridView grid,
            [NotNull] IList <TRow> rows,
            bool autoGenerateColumns = false,
            [CanBeNull] DataGridViewSortState sortStateOverride = null,
            DataGridViewSortState defaultSortState = null,
            [CanBeNull] BoundDataGridSelectionState <TRow> selectionStateOverride = null)
            where TRow : class
        {
            Assert.ArgumentNotNull(grid, nameof(grid));
            Assert.ArgumentNotNull(rows, nameof(rows));

            var sortState = sortStateOverride ??
                            (defaultSortState != null &&
                             (grid.SortedColumn == null ||
                              grid.SortOrder == SortOrder.None)
                                                 ? defaultSortState
                                                 : new DataGridViewSortState(grid));

            var  bindingList = rows as IBindingList;
            bool presorted   = bindingList != null &&
                               TrySortBindingList <TRow>(bindingList, grid, sortState);

            var selectionState = selectionStateOverride ?? GetSelectionState <TRow>(grid);

            grid.AutoGenerateColumns = autoGenerateColumns;

            grid.DataSource = typeof(TRow);
            grid.DataSource = rows;

            bool sortingApplied = presorted || TryApplySortState(grid, sortState);

            RestoreSelection(grid, selectionState);

            return(sortingApplied);
        }