public override List<Row> GetData(GridContext context, out int? totalRecords)
        {
            List<Row> resultRows = new List<Row>();
            if (RetrieveData == null)
            {
                totalRecords = 0;
                return resultRows;
            }

            var queryResult = RetrieveData(context);
            totalRecords = queryResult.TotalRecords;

            if (context.GridDefinition.Paging && !totalRecords.HasValue)
            {
                throw new Exception("When paging is enabled, QueryResult must contain the TotalRecords");
            }

            IMVCGridTemplatingEngine templatingEngine = (IMVCGridTemplatingEngine)Activator.CreateInstance(context.GridDefinition.TemplatingEngine, true);

            foreach (var item in queryResult.Items)
            {
                Row thisRow = new Row();

                if (RowCssClassExpression != null)
                {
                    string rowCss = RowCssClassExpression(item, context);
                    if (!String.IsNullOrWhiteSpace(rowCss))
                    {
                        thisRow.CalculatedCssClass = rowCss;
                    }
                }

                foreach (var col in this.Columns)
                {
                    Cell thisCell = new Cell();
                    thisRow.Cells.Add(col.ColumnName, thisCell);

                    thisCell.HtmlText = "";

                    if (col.ValueExpression != null)
                    {
                        thisCell.HtmlText = col.ValueExpression(item, context);
                    }
                    
                    if (!String.IsNullOrWhiteSpace(col.ValueTemplate))
                    {
                        var templateModel = new TemplateModel()
                        {
                            Item = item,
                            GridContext = context,
                            GridColumn = col,
                            Row = thisRow,
                            Value = thisCell.HtmlText
                        };

                        thisCell.HtmlText = templatingEngine.Process(col.ValueTemplate, templateModel);
                    }

                    if (col.HtmlEncode)
                    {
                        thisCell.HtmlText = System.Web.HttpUtility.HtmlEncode(thisCell.HtmlText);
                    }

                    thisCell.PlainText = thisCell.HtmlText;
                    if (col.PlainTextValueExpression != null)
                    {
                        thisCell.PlainText = col.PlainTextValueExpression(item, context);
                    }

                    if (col.CellCssClassExpression != null)
                    {
                        string cellCss = col.CellCssClassExpression(item, context);
                        if (!String.IsNullOrWhiteSpace(cellCss))
                        {
                            thisCell.CalculatedCssClass = cellCss;
                        }
                    }
                }

                resultRows.Add(thisRow);
            }

            return resultRows;
        }
示例#2
0
        internal override List <Row> GetData(GridContext context, out int?totalRecords)
        {
            var resultRows = new List <Row>();

            var queryResult = RetrieveData(context);

            totalRecords = queryResult.TotalRecords;

            if (context.GridDefinition.Paging && !totalRecords.HasValue)
            {
                throw new Exception("When paging is enabled, QueryResult must contain the TotalRecords");
            }

            var templatingEngine    = (IMVCGridTemplatingEngine)Activator.CreateInstance(context.GridDefinition.TemplatingEngine, true);
            var rowSelectProperties = GetRowSelectProperties(context);

            foreach (var item in queryResult.Items)
            {
                var thisRow = new Row
                {
                    CalculatedCssClass       = String.Empty,
                    RowSelectEventParameters = rowSelectProperties != null?GetEventParameters(rowSelectProperties, item) : null
                };

                if (!String.IsNullOrEmpty(RowCssClass))
                {
                    thisRow.CalculatedCssClass = RowCssClass.Trim();
                }

                if (RowCssClassExpression != null)
                {
                    string rowCss = RowCssClassExpression(item, context);
                    if (!String.IsNullOrWhiteSpace(rowCss))
                    {
                        thisRow.CalculatedCssClass = String.Join(" ", thisRow.CalculatedCssClass, rowCss.Trim());
                    }
                }

                foreach (var col in this.Columns)
                {
                    Cell thisCell = new Cell
                    {
                        CalculatedCssClass = String.Empty
                    };
                    thisRow.Cells.Add(col.ColumnName, thisCell);

                    thisCell.HtmlText = "";

                    if (col.ValueExpression != null)
                    {
                        thisCell.HtmlText = col.ValueExpression(item, context);
                    }

                    if (!String.IsNullOrWhiteSpace(col.ValueTemplate))
                    {
                        var templateModel = new TemplateModel()
                        {
                            Item        = item,
                            GridContext = context,
                            GridColumn  = col,
                            Row         = thisRow,
                            Value       = thisCell.HtmlText
                        };

                        thisCell.HtmlText = templatingEngine.Process(col.ValueTemplate, templateModel);
                    }

                    if (col.HtmlEncode)
                    {
                        thisCell.HtmlText = System.Web.HttpUtility.HtmlEncode(thisCell.HtmlText);
                    }

                    thisCell.PlainText = thisCell.HtmlText;
                    if (col.PlainTextValueExpression != null)
                    {
                        thisCell.PlainText = col.PlainTextValueExpression(item, context);
                    }

                    if (!String.IsNullOrEmpty(col.CellCssClass))
                    {
                        thisCell.CalculatedCssClass = col.CellCssClass.Trim();
                    }

                    if (col.CellCssClassExpression != null)
                    {
                        string cellCss = col.CellCssClassExpression(item, context);
                        if (!String.IsNullOrWhiteSpace(cellCss))
                        {
                            thisCell.CalculatedCssClass = String.Join(" ", thisCell.CalculatedCssClass, cellCss.Trim());
                        }
                    }
                }

                resultRows.Add(thisRow);
            }

            return(resultRows);
        }