Пример #1
0
        public static IEnumerable ToDataSource(this DataGrid grid, IEnumerable <ExpandoObject> list, ExpandoObject firstObj)
        {
            ExpandoObject first = firstObj;

            if (first == null)
            {
                var numer = list.GetEnumerator();
                numer.Reset();
                numer.MoveNext();
                first = numer.Current;
                numer.Reset();
            }
            ObservableCollection <DataGridColumn> columns = grid.Columns;

            columns.Clear();
            foreach (var key in ExpandoUtils.Keys(first))
            {
                var item = new DataGridTextColumn()
                {
                    Header  = key,
                    Binding = new Binding(key)
                };
                columns.Add(item);
            }

            // ItemsControl
            list.GetEnumerator(); // .Reset();
            IEnumerable result = list as IEnumerable;

            grid.ItemsSource = result;
            return(result);
        }
Пример #2
0
        public static T Convert <T>(IEnumerable <ExpandoObject> list) where T : class
        {
            XElement table = new XElement("table", new XAttribute("style", "border: 1px solid gray;"));
            XElement head  = new XElement("tr");

            var numerator = list.GetEnumerator();

            if (numerator == null)
            {
                return(default(T));
            }
            if (!numerator.MoveNext())
            {
                return(default(T));
            }
            ExpandoObject first = numerator.Current;

            if (first == null)
            {
                return(default(T));
            }

            // Ai.Reflection;
            foreach (string item in ExpandoUtils.Keys(first))
            {
                head.Add(new XElement("th",
                                      new XElement("nobr", HtmlAgilityPack.HtmlDocument.HtmlEncode(item))
                                      ));
            }
            table.Add(head);

            do
            {
                var item = numerator.Current as ExpandoObject;
                var row  = new XElement("tr");
                foreach (string itemKey in item.Keys())
                {
                    var    td            = new XElement("td");
                    string itemValueOrig = item.DynValue <string>(itemKey);
                    if (!string.IsNullOrWhiteSpace(itemValueOrig))
                    {
                        var itemValue = itemValueOrig.TrimEnd();
                        if (itemValue.Contains(' ') && itemValue.Length < 30)
                        {
                            // HtmlAgilityPack.HtmlDocument.HtmlEncode
                            td.Add(new XElement("nobr", (itemValue)));
                        }
                        else if (itemValue.Length > 170)
                        {
                            td.Add(new XElement("div"
                                                , new XAttribute("style", "min-width: 400px;")
                                                , itemValue));
                        }
                        else if (itemValue.Length > 70)
                        {
                            td.Add(new XElement("div"
                                                , new XAttribute("style", "min-width: 200px;")
                                                , itemValue));
                        }
                        else
                        {
                            td.Value = (itemValue);
                        }
                    }
                    row.Add(td);
                }
                table.Add(row);
            }while (numerator.MoveNext());

            // body.Add(table); return body as T;
            return(table as T);
        }