コード例 #1
0
        public static Table <T> AddColumn <T, TValue>(
            this Table <T> table,
            Expression <Func <T, TValue> > expression = null,
            string title  = null,
            Type dataType = null,
            string format = null,
            bool?convertEmptyStringToNull = null,
            bool?encodeValue        = null,
            string nullDisplayText  = null,
            CultureInfo culture     = null,
            Func <T, object> select = null)
        {
            var tableColumn = new TableColumn <T>();

            if (expression != null)
            {
                var metadata = Metadata.Create(expression);
                tableColumn.DataType = dataType ?? (metadata.DataType == null ? typeof(string) : ConvertUtilities.GetType(metadata.DataType)) ?? typeof(string);
                tableColumn.Format   = format ?? metadata.DisplayFormat;
                tableColumn.Title    = title ?? metadata.DisplayName;
                tableColumn.ConvertEmptyStringToNull = convertEmptyStringToNull ?? metadata.ConvertEmptyStringToNull;
                tableColumn.NullDisplayText          = nullDisplayText ?? metadata.NullDisplayText;
                tableColumn.EncodeValue = encodeValue ?? metadata.EncodeValue;
            }
            else
            {
                tableColumn.DataType = dataType ?? typeof(string);
                tableColumn.Format   = format;
                tableColumn.Title    = title;
                if (convertEmptyStringToNull.HasValue)
                {
                    tableColumn.ConvertEmptyStringToNull = convertEmptyStringToNull.Value;
                }
                tableColumn.NullDisplayText = nullDisplayText;
                if (encodeValue.HasValue)
                {
                    tableColumn.EncodeValue = encodeValue.Value;
                }
            }

            if (culture != null)
            {
                tableColumn.Culture = culture;
            }

            if (select == null && expression != null)
            {
                var func = expression.Compile();
                tableColumn.SelectFunction = obj => func(obj);
            }
            else
            {
                tableColumn.SelectFunction = select;
            }

            table.Columns.Add(tableColumn);
            return(table);
        }