/// <summary>
        /// Binds the grid.
        /// </summary>
        private void BindGrid()
        {
            var dataViewId = hfDataViewId.Value.AsIntegerOrNull();

            pnlResultsGrid.Visible = false;

            pnlView.Visible = false;
            if (!dataViewId.HasValue)
            {
                return;
            }

            var dataView = new DataViewService(new RockContext()).Get(dataViewId.Value);

            if (dataView == null)
            {
                return;
            }

            if (!dataView.EntityTypeId.HasValue)
            {
                return;
            }

            var dataViewEntityType = EntityTypeCache.Get(dataView.EntityTypeId.Value);

            if (dataViewEntityType == null || dataViewEntityType.AssemblyName == null)
            {
                return;
            }

            Type dataViewEntityTypeType = dataViewEntityType.GetEntityType();

            if (dataViewEntityTypeType == null)
            {
                return;
            }

            pnlView.Visible = true;

            gDataViewResults.DataSource = null;

            // Only respect the ShowResults option if fetchRowCount is null
            var showResults = GetBlockUserPreference(UserPreferenceKey.ShowResults).AsBooleanOrNull() ?? true;

            if (showResults)
            {
                btnToggleResults.Text    = "Hide Results <i class='fa fa-chevron-up'></i>";
                btnToggleResults.ToolTip = "Hide Results";
                btnToggleResults.RemoveCssClass("btn-primary");
                btnToggleResults.AddCssClass("btn-default");
            }
            else
            {
                btnToggleResults.Text = "Show Results <i class='fa fa-chevron-down'></i>";
                btnToggleResults.RemoveCssClass("btn-default");
                btnToggleResults.AddCssClass("btn-primary");
                btnToggleResults.ToolTip = "Show Results";
            }

            if (!showResults)
            {
                return;
            }

            if (!dataView.IsAuthorized(Authorization.VIEW, CurrentPerson))
            {
                return;
            }

            gDataViewResults.EntityTypeId = dataView.EntityTypeId;
            bool isPersonDataSet = dataView.EntityTypeId == EntityTypeCache.GetId <Rock.Model.Person>();

            if (isPersonDataSet)
            {
                gDataViewResults.PersonIdField = "Id";
                gDataViewResults.DataKeyNames  = new string[] { "Id" };
            }
            else
            {
                gDataViewResults.PersonIdField = null;
            }

            var entityTypeCache = EntityTypeCache.Get(dataView.EntityTypeId.Value);

            if (entityTypeCache != null)
            {
                gDataViewResults.RowItemText = entityTypeCache.FriendlyName;
            }

            pnlResultsGrid.Visible = true;

            var enableCountingDataViewStatistics = this.GetAttributeValue(AttributeKey.EnableCountingDataViewStatistics).AsBooleanOrNull() ?? true;

            try
            {
                gDataViewResults.CreatePreviewColumns(dataViewEntityTypeType);
                var dbContext            = dataView.GetDbContext();
                var dataViewGetQueryArgs = new DataViewGetQueryArgs
                {
                    SortProperty            = gDataViewResults.SortProperty,
                    DbContext               = dbContext,
                    DatabaseTimeoutSeconds  = GetAttributeValue(AttributeKey.DatabaseTimeoutSeconds).AsIntegerOrNull() ?? 180,
                    DataViewFilterOverrides = new DataViewFilterOverrides
                    {
                        ShouldUpdateStatics = enableCountingDataViewStatistics
                    }
                };

                var qry = dataView.GetQuery(dataViewGetQueryArgs);

                gDataViewResults.SetLinqDataSource(qry.AsNoTracking());
                gDataViewResults.DataBind();
            }
            catch (Exception ex)
            {
                this.LogException(ex);
                var sqlTimeoutException = ReportingHelper.FindSqlTimeoutException(ex);
                var errorBox            = nbGridError;

                if (sqlTimeoutException != null)
                {
                    errorBox.NotificationBoxType = NotificationBoxType.Warning;
                    errorBox.Text = "This data view did not complete in a timely manner. You can try again or adjust the timeout setting of this block.";
                    return;
                }
                else
                {
                    if (ex is RockDataViewFilterExpressionException)
                    {
                        RockDataViewFilterExpressionException rockDataViewFilterExpressionException = ex as RockDataViewFilterExpressionException;
                        errorBox.Text = rockDataViewFilterExpressionException.GetFriendlyMessage(dataView);
                    }
                    else
                    {
                        errorBox.Text = "There was a problem with one of the filters for this data view.";
                    }

                    errorBox.NotificationBoxType = NotificationBoxType.Danger;

                    errorBox.Details = ex.Message;
                    errorBox.Visible = true;
                    return;
                }
            }

            gDataViewResults.RowItemText = dataViewEntityType.FriendlyName;

            if (gDataViewResults.DataSource != null)
            {
                gDataViewResults.ExportFilename = dataView.Name;
            }
        }