Exemplo n.º 1
0
        protected void UpdateColumnFormats(bool restoreDefaultFormats)
        {
            var bindingListSource = DataSource as BindingListSource;

            if (bindingListSource == null)
            {
                return;
            }
            var newColumnFormats = ImmutableList.ValueOf(_itemProperties.Select(prop => bindingListSource.ColumnFormats.GetFormat(new ColumnId(prop.ColumnCaption))));

            if (Equals(newColumnFormats, _columnFormats))
            {
                return;
            }
            _columnFormats = newColumnFormats;
            foreach (var column in Columns.OfType <DataGridViewColumn>())
            {
                if (string.IsNullOrEmpty(column.DataPropertyName))
                {
                    continue;
                }
                DataPropertyDescriptor pd           = null;
                ColumnFormat           columnFormat = null;
                if (column.Index < _itemProperties.Count && _itemProperties[column.Index].Name == column.DataPropertyName)
                {
                    pd           = _itemProperties[column.Index];
                    columnFormat = _columnFormats[column.Index];
                }
                else
                {
                    for (int i = 0; i < _itemProperties.Count; i++)
                    {
                        if (_itemProperties[i].Name == column.DataPropertyName)
                        {
                            pd           = _itemProperties[i];
                            columnFormat = _columnFormats[i];
                        }
                    }
                }
                if (pd == null)
                {
                    continue;
                }
                if (null != columnFormat.Format)
                {
                    column.DefaultCellStyle.Format = columnFormat.Format;
                }
                else
                {
                    if (restoreDefaultFormats)
                    {
                        var originalColumn = _viewContext.CreateGridViewColumn(pd);
                        column.DefaultCellStyle.Format = originalColumn.DefaultCellStyle.Format;
                    }
                }
                if (columnFormat.Width.HasValue)
                {
                    column.Width = columnFormat.Width.Value;
                }
            }
        }