Beispiel #1
0
        internal void SetCellMetaData(GridCell cell, int row, int column)
        {
            cell.Row    = row;
            cell.Column = column;
            var headerName  = ControlColumnDataCollection[column].HeaderName;
            var controlData = ControlColumnDataCollection.GetControlColumnDataByHeaderName(headerName.Trim());

            cell.CellControlBy          = controlData.By;
            cell.CellControlElementType = controlData.ElementType;
        }
Beispiel #2
0
        public IList <GridCell> GetColumn(string header)
        {
            int?position = HeaderNamesService.GetHeaderPosition(header, ControlColumnDataCollection.AsEnumerable <IHeaderInfo>().ToList());

            if (position == null)
            {
                return(new List <GridCell>());
            }

            return(GetColumn((int)position));
        }
Beispiel #3
0
        public GridCell GetCell(string header, int row)
        {
            int?position = HeaderNamesService.GetHeaderPosition(header, ControlColumnDataCollection.AsEnumerable <IHeaderInfo>().ToList());

            if (position == null)
            {
                return(null);
            }

            return(GetCell(row, (int)position));
        }
Beispiel #4
0
        private ControlColumnData GetControlDataByProperty(PropertyInfo property)
        {
            Attribute   headerNameAttribute = property.GetCustomAttributes(typeof(HeaderNameAttribute)).FirstOrDefault();
            IHeaderInfo headerInfo;

            if (headerNameAttribute != null)
            {
                headerInfo = ControlColumnDataCollection.FirstOrDefault(h => h.HeaderName == ((HeaderNameAttribute)headerNameAttribute).Name);
            }
            else
            {
                headerInfo = ControlColumnDataCollection.FirstOrDefault(h => h.HeaderName == property.Name);
            }

            return(headerInfo as ControlColumnData);
        }
Beispiel #5
0
        public TRowObject CastRow <TRowObject>(int rowIndex, params string[] propertiesToSkip)
            where TRowObject : new()
        {
            var cells = TableService.GetRowCells(rowIndex);

            if (cells.Count != ControlColumnDataCollection.Count)
            {
                // Compare headers to determine why the cells count is different
                var actual   = TableService.Headers.Select(c => c.InnerText.Trim(" 0".ToCharArray())).ToList();
                var expected = ControlColumnDataCollection.Select(c => c.HeaderName).ToList();
                CollectionAssert.AreEqual(expected, actual, $"Expected: {expected.Stringify()}\r\nActual: {actual.Stringify()}");
            }

            var dto        = new TRowObject();
            var properties = dto.GetType().GetProperties().ToList();

            foreach (var propertyInfo in properties)
            {
                string headerName = HeaderNamesService.GetHeaderNameByProperty(propertyInfo);

                if (propertiesToSkip.Contains(headerName))
                {
                    continue;
                }

                int?headerPosition = HeaderNamesService.GetHeaderPosition(headerName, ControlColumnDataCollection.AsEnumerable <IHeaderInfo>().ToList());
                if (headerPosition == null)
                {
                    continue;
                }

                var controlData = GetControlDataByProperty(propertyInfo);
                if (controlData != null && controlData.ElementType != null && controlData.ElementType.IsSubclassOf(typeof(Element)))
                {
                    var     repo      = new ElementRepository();
                    var     xpath     = $".{cells[(int)headerPosition].GetXPath()}";
                    var     tableCell = this.CreateByXpath <TableCell>(xpath);
                    dynamic elementValue;
                    if (controlData.By == null)
                    {
                        controlData.By = new FindXpathStrategy(xpath);
                        elementValue   = CastCell(repo, controlData, tableCell);
                        controlData.By = null;
                    }
                    else
                    {
                        elementValue = CastCell(repo, controlData, tableCell);
                    }

                    var elementType = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType;

                    var newValue = elementValue == null ? default : Convert.ChangeType(elementValue, elementType);

                                   elementValue = Convert.ChangeType(newValue, propertyInfo.PropertyType);
                                   propertyInfo.SetValue(dto, elementValue);
                }
                else
                {
                    string htmlNodeValue = HttpUtility.HtmlDecode(TableService.GetRowCells(rowIndex)[(int)headerPosition].InnerText).Trim();
                    var    type          = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType;
                    object elementValue;
                    if (type == typeof(DateTime) || type == typeof(DateTime?))
                    {
                        DateTime dateTime;
                        DateTime.TryParse(htmlNodeValue, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out dateTime);
                        elementValue = (DateTime?)dateTime;
                    }
                    else
                    {
                        elementValue = string.IsNullOrEmpty(htmlNodeValue) ? default : Convert.ChangeType(htmlNodeValue, type, CultureInfo.InvariantCulture);
                    }

                    propertyInfo.SetValue(dto, elementValue);
                }
            }

            return(dto);
        }