Exemple #1
0
        internal T CastRow <T>(TableRow row)
            where T : new()
        {
            var props   = typeof(T).GetProperties();
            var castRow = new T();

            foreach (var propertyInfo in props)
            {
                if (row.GetCells().Any())
                {
                    if (propertyInfo.CanWrite)
                    {
                        string headerName     = HeaderNamesService.GetHeaderNameByProperty(propertyInfo);
                        int?   headerPosition = HeaderNamesService.GetHeaderPosition(headerName, ColumnHeaderNames.AsEnumerable <IHeaderInfo>().ToList());

                        // Will skip properties that are not part of the meta data.
                        if (headerPosition != null)
                        {
                            dynamic elementValue = row.GetCells().ToElementList()[(int)headerPosition].InnerText;
                            propertyInfo.SetValue(castRow, elementValue, null);
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException("Cannot cast grid data to C# object. Most probably your C# class properties don't have setters.");
                    }
                }
            }

            return(castRow);
        }
Exemple #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));
        }
Exemple #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));
        }
Exemple #4
0
        public TableCell GetCell(string header, int row)
        {
            int?position = HeaderNamesService.GetHeaderPosition(header, ColumnHeaderNames.AsEnumerable <IHeaderInfo>().ToList());

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

            return(GetCell((int)position, row));
        }
Exemple #5
0
        public GridCell GetCell <TDto>(Expression <Func <TDto, object> > expression, int row)
            where TDto : class
        {
            string headerName = HeaderNamesService.GetHeaderNameByExpression <TDto>(expression);
            int?   position   = HeaderNamesService.GetHeaderPosition(headerName, ControlColumnDataCollection);

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

            return(GetCell(row, (int)position));
        }
Exemple #6
0
        public TableCell GetCell <TDto>(Expression <Func <TDto, object> > expression, int row)
            where TDto : class
        {
            string headerName = HeaderNamesService.GetHeaderNameByExpression <TDto>(expression);
            int?   position   = HeaderNamesService.GetHeaderPosition(headerName, ColumnHeaderNames.AsEnumerable <IHeaderInfo>().ToList());

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

            return(GetCell((int)position, row));
        }
Exemple #7
0
 public void SetParentTable(Table table)
 {
     _parentTable        = table;
     _headerNamesService = new HeaderNamesService(_parentTable.TableService.HeaderRows);
 }
Exemple #8
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);
        }
Exemple #9
0
        public int GetGridColumnIndexByName(string columnName)
        {
            var coll = HeaderNamesService.GetHeaderNames();

            return(coll.IndexOf(columnName));
        }
Exemple #10
0
 public string GetGridColumnNameByIndex(int index) => HeaderNamesService.GetHeaderNames()[index];
Exemple #11
0
 public IList <string> GetHeaderNames()
 {
     return(HeaderNamesService.GetHeaderNames());
 }