Exemple #1
0
        public WebGrid Bind(
            IEnumerable <dynamic> source,
            IEnumerable <string> columnNames = null,
            bool autoSortAndPage             = true,
            int rowCount = -1
            )
        {
            if (_dataSourceBound)
            {
                throw new InvalidOperationException(HelpersResources.WebGrid_DataSourceBound);
            }
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (!autoSortAndPage && _canPage && rowCount == -1)
            {
                throw new ArgumentException(
                          HelpersResources.WebGrid_RowCountNotSpecified,
                          "rowCount"
                          );
            }

            _elementType = GetElementType(source);
            if (_columnNames == null)
            {
                _columnNames =
                    columnNames ?? GetDefaultColumnNames(source, elementType: _elementType);
            }

            if (!autoSortAndPage)
            {
                _dataSource = new PreComputedGridDataSource(
                    grid: this,
                    values: source,
                    totalRows: rowCount
                    );
            }
            else
            {
                WebGridDataSource dataSource = new WebGridDataSource(
                    grid: this,
                    values: source,
                    elementType: _elementType,
                    canPage: _canPage,
                    canSort: _canSort
                    );
                dataSource.DefaultSort = new SortInfo
                {
                    SortColumn    = _defaultSort,
                    SortDirection = SortDirection.Ascending
                };
                dataSource.RowsPerPage = _rowsPerPage;
                _dataSource            = dataSource;
            }
            _dataSourceBound = true;
            ValidatePreDataBoundValues();
            return(this);
        }
Exemple #2
0
        public WebGrid Bind(IEnumerable<dynamic> source, IEnumerable<string> columnNames = null, bool autoSortAndPage = true, int rowCount = -1) {
            if (_dataSourceBound) {
                throw new InvalidOperationException(HelpersResources.WebGrid_DataSourceBound);
            }
            if (source == null) {
                throw new ArgumentNullException("source");
            }
            if (!autoSortAndPage && _canPage && rowCount == -1) {
                throw new ArgumentException(HelpersResources.WebGrid_RowCountNotSpecified, "rowCount");
            }

            _elementType = GetElementType(source);
            if (_columnNames == null) {
                _columnNames = columnNames ?? GetDefaultColumnNames(source, elementType: _elementType);
            }

            if (!autoSortAndPage) {
                _dataSource = new PreComputedGridDataSource(grid: this, values: source, totalRows: rowCount);
            }
            else {
                WebGridDataSource dataSource = new WebGridDataSource(grid: this, values: source, elementType: _elementType, canPage: _canPage, canSort: _canSort);
                dataSource.DefaultSort = new SortInfo { SortColumn = _defaultSort, SortDirection = SortDirection.Ascending };
                dataSource.RowsPerPage = _rowsPerPage;
                _dataSource = dataSource;
            }
            _dataSourceBound = true;
            ValidatePreDataBoundValues();
            return this;
        }