Example #1
0
        /// <summary>
        /// Transforms the given dictionary.
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private DataTable Transform(ITypedList data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            if (!(data is IEnumerable))
            {
                throw new ArgumentException(string.Format("data is of type '{0}', not IEnumerable.", data.GetType()));
            }

            // define a table...
            DataTable table = this.CreateDataTable();

            // get the props...
            PropertyDescriptorCollection properties = data.GetItemProperties(new PropertyDescriptor[] {});

            if (properties == null)
            {
                throw new InvalidOperationException("properties is null.");
            }

            // create columns...
            foreach (PropertyDescriptor property in properties)
            {
                // create...
                DataColumn column = new DataColumn(property.Name, property.PropertyType);
                table.Columns.Add(column);
            }

            // do the rows...
            object[] values = new object[properties.Count];
            foreach (object obj in (IEnumerable)data)
            {
                // get the values...
                for (int index = 0; index < properties.Count; index++)
                {
                    // get a value...
                    object value = properties[index].GetValue(obj);
                    values[index] = value;
                }

                // add...
                table.Rows.Add(values);
            }

            // return...
            return(table);
        }