VerticalGrid垂直网格示例

How to Create a toolbar for a PropertyGridControl that allows properties to be sorted alphabetically
  1. 1、怎样为属性网格控件创建工具栏以允许属性按照字母排序

CodeCentralShow Me

The complete sample project is available in the DevExpress Code Central database athttp://www.devexpress.com/example=E504. Depending on the target platform type (ASP.NET, WinForms, etc), you can either run this example online or download an auto-executable sample.

In the example a bar with two buttons is created above a PropertyGridControl. The buttons are used to sort properties alphabetically and group properties by categories, respectively.

示例中,带有两个按钮的工具条将会添加到属性网格控件上方。两个按钮分别用来对属性进行字母表排序并根据类别进行分组。
To change the order of properties, the PropertyGridControl's OptionsView.ShowRootCategories property is used.

要改变属性顺序,属性网格控件的OptionsView.ShowRootCategories属性将会使用。

The following image shows the result:

C#:Form1.cs
VB:Form1.vb
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraVerticalGrid;
using DevExpress.Utils;
using DevExpress.XtraBars;
using System.Reflection;

namespace Sample___Toolbar {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
            this.barButtonCategorized.DownChanged += this.barButton_DownChanged;
            this.barButtonAlphabetical.DownChanged += this.barButton_DownChanged;
            SetBarButtonToolTip(this.barButtonAlphabetical, "Alphabetical");
            SetBarButtonToolTip(this.barButtonCategorized, "Categorized");
            this.barButtonCategorized.Glyph = ResourceImageHelper.CreateImageFromResources("Sample___Toolbar.Categorized.png", Assembly.GetExecutingAssembly());
            this.barButtonAlphabetical.Glyph = ResourceImageHelper.CreateImageFromResources("Sample___Toolbar.Alphabetical.png", Assembly.GetExecutingAssembly());
            this.ShowCategories = true;
            PropertyGridControl.SelectedObject = PropertyGridControl;
        }
        public bool ShowCategories {
            get { return this.barButtonCategorized.Down; }
            set { this.barButtonCategorized.Down = value; }
        }
        public PropertyGridControl PropertyGridControl {
            get { return propertyGridControl; }
        }
        private void barButton_DownChanged(object sender, ItemClickEventArgs e) {
            if(e.Item.Equals(this.barButtonCategorized)) {
                SetBarButtonDown(this.barButtonAlphabetical, !this.barButtonCategorized.Down);
            } else {
                SetBarButtonDown(this.barButtonCategorized, !this.barButtonAlphabetical.Down);
            }
            UpdatePropertyGrid();
        }
        static void SetBarButtonToolTip(DevExpress.XtraBars.BarButtonItem barButton, string value) {
            SuperToolTip superToolTip = new SuperToolTip();
            ToolTipTitleItem toolTipTitleItem = new ToolTipTitleItem();
            toolTipTitleItem.Text = value;
            superToolTip.Items.Add(toolTipTitleItem);
            barButton.SuperTip = superToolTip;
        }
        void SetBarButtonDown(BarButtonItem barButton, bool value) {
            barButton.DownChanged -= new ItemClickEventHandler(this.barButton_DownChanged);
            barButton.Down = value;
            barButton.DownChanged += new ItemClickEventHandler(this.barButton_DownChanged);
        }
        void UpdatePropertyGrid() {
            this.propertyGridControl.OptionsView.ShowRootCategories = this.barButtonCategorized.Down;
        }
    }
}


How to: Edit properties of multiple objects in a single PropertyGridControl
  1. 2、怎样在单一的属性网格控件中编辑多个对象的属性

CodeCentralShow Me

The complete sample project is available in the DevExpress Code Central database athttp://www.devexpress.com/example=E2253. Depending on the target platform type (ASP.NET, WinForms, etc), you can either run this example online or download an auto-executable sample.

This example shows how to edit properties of different objects in a single PropertyGridControl. Properties to be edited are provided via the CustomPropertyDescriptors event.

要编辑的属性由CustomPropertyDescriptors事件提供。

C#:Form1.cs
 
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraVerticalGrid.Events;

namespace CustomProperties {
    public partial class Form1 : Form {
        List<PropertyDescriptor> propertyStore = new List<PropertyDescriptor>();
        public Form1() {
            InitializeComponent();
            propertyStore.Add(new CustomPropertyDescriptor(this, "Form's Size", "Size"));
            propertyStore.Add(new CustomPropertyDescriptor(this, "Form's Title", "Text"));
            propertyStore.Add(new CustomPropertyDescriptor(this.propertyGridControl1, "PropertyGridControl's RecordWidth", "RecordWidth"));
            propertyStore.Add(new CustomPropertyDescriptor(this.button1, "Button's Visibility", "Visible"));
            this.propertyGridControl1.SelectedObject = propertyStore;
        }

        void propertyGridControl1_CustomPropertyDescriptors(object sender, CustomPropertyDescriptorsEventArgs e) {
            if(e.Source == propertyStore) {
                PropertyDescriptorCollection rootProperties = new PropertyDescriptorCollection(null);
                foreach(PropertyDescriptor pd in propertyStore) {
                    rootProperties.Add(pd);
                }
                e.Properties = rootProperties;
            }
        }
    }
    class CustomPropertyDescriptor : PropertyDescriptor {
        string name;
        PropertyDescriptor sourcePropertyDescriptor;
        object source;

        public CustomPropertyDescriptor(object source, string name, string targetPath) : base(name, null) {
            this.name = name;
            this.source = source;
            this.sourcePropertyDescriptor = TypeDescriptor.GetProperties(source)[targetPath];
            if(SourcePropertyDescriptor == null)
                throw new Exception("Can't bind to the source with the " + targetPath + " property");
        }

        public override string Name { get { return name; } }
        public override Type ComponentType { get { return SourcePropertyDescriptor.ComponentType; } }
        public override bool IsReadOnly { get { return SourcePropertyDescriptor.IsReadOnly; } }
        public override Type PropertyType { get { return SourcePropertyDescriptor.PropertyType; } }

        PropertyDescriptor SourcePropertyDescriptor { get { return sourcePropertyDescriptor; } }
        object Source { get { return source; } }

        public override object GetValue(object component) {
            return SourcePropertyDescriptor.GetValue(Source);
        }
        public override bool CanResetValue(object component) {
            return SourcePropertyDescriptor.CanResetValue(Source);
        }
        public override void ResetValue(object component) {
            SourcePropertyDescriptor.ResetValue(Source);
        }
        public override void SetValue(object component, object value) {
            SourcePropertyDescriptor.SetValue(Source, value);
        }
        public override bool ShouldSerializeValue(object component) {
            return SourcePropertyDescriptor.ShouldSerializeValue(Source);
        }
    }
}



How to: Filter an object's properties using the CustomPropertyDescriptors event

 

3、如何:通过使用CustomPropertyDescriptors事件过滤对象属性

CodeCentralShow Me

The complete sample project is available in the DevExpress Code Central database at http://www.devexpress.com/example=E2254. Depending on the target platform type (ASP.NET, WinForms, etc), you can either run this example online or download an auto-executable sample.

This example shows how to display specific properties in a PropertyGridControl, while hiding other properties. Properties to be displayed are filtered via the CustomPropertyDescriptors event.

At the root level the Dock, Size and Location properties are displayed. For the Size property, only the Height nested property is displayed.

C#:Form1.cs
VB:Form1.vb
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraVerticalGrid.Events;

namespace PropertyFiltering {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
            this.propertyGridControl1.SelectedObject = this.propertyGridControl1;
            this.propertyGridControl1.GetRowByFieldName("Size").Expanded = true;
            this.propertyGridControl1.GetRowByFieldName("Location").Expanded = true;
        }

        void propertyGridControl1_CustomPropertyDescriptors(object sender, CustomPropertyDescriptorsEventArgs e) {
            // Provide properties to be displayed at the root level
            if(e.Context.PropertyDescriptor == null) {
                PropertyDescriptorCollection filteredCollection = new PropertyDescriptorCollection(null);
                AddIfPropertyExist(e.Properties, filteredCollection, "Dock");
                AddIfPropertyExist(e.Properties, filteredCollection, "Size");
                AddIfPropertyExist(e.Properties, filteredCollection, "Location");
                AddIfPropertyExist(e.Properties, filteredCollection, "NonexistentProperty");
                e.Properties = filteredCollection;
            }
            //Provide nested properties for the Size property
            if(e.Context.PropertyDescriptor != null && e.Context.PropertyDescriptor.Name == "Size") {
                PropertyDescriptorCollection filteredCollection = new PropertyDescriptorCollection(null);
                AddIfPropertyExist(e.Properties, filteredCollection, "Height");
                e.Properties = filteredCollection;
            }
        }
        void AddIfPropertyExist(PropertyDescriptorCollection sourceCollection, PropertyDescriptorCollection targetCollection, string name) {
            PropertyDescriptor foundPropertyDescriptor = sourceCollection[name];
            if(foundPropertyDescriptor == null)
                return;
            targetCollection.Add(foundPropertyDescriptor);
        }
    }
}



PropertyDescriptor aa= sourceCollection[name];
其中name为属性名如Size,sourceCollection 为属性描述集合,根据属性名可以得到属性描述集合中的相应属性描述项。
How to: Format Values in the XtraVerticalGrid Using Standard Format Strings

 

4、如何:在XtraVerticalGrid中使用标准格式化字符串格式化行中的值

The following example demonstrates how to format values in the VerticalGrid's rows using standard format strings. These are described in the Format Specifiers topic.

Values in the rowPrice row will be formatted as a currency amount. Values in the rowDeliveryDate row will be represented in the Long Date form.

rowPrice行中的值将会被格式化为货币金额。rowDeliveryDate中的值将会被格式化为Long Date格式。

The image below shows the result.

using DevExpress.Utils;
// ...
rowPrice.Properties.Format.FormatType = FormatType.Numeric;
rowPrice.Properties.Format.FormatString = "c2";
rowDeliveryDate.Properties.Format.FormatType = FormatType.DateTime;
rowDeliveryDate.Properties.Format.FormatString = "D";

How to: Print a Vertical Grid and Show its Print Preview

 

4、如何:打印垂直网格并显示打印预览

The following example demonstrates how to print a Vertical Grid or show its Print Preivew. To do this, you should use either the VGridControlBase.Print or VGridControlBase.ShowPrintPreview methods.

NoteNote

The Vertical Grid can be printed and previewed only if the XtraPrinting Library is available. To verify that printing the Vertical Grid is possible, use the VGridControlBase.IsPrintingAvailable property.

C#
VB
using DevExpress.XtraVerticalGrid;
// ...

private void ShowVerticalGridPreview(VGridControl vGrid) {
    // Check whether the Vertical Grid can be previewed.
    if (!vGrid.IsPrintingAvailable) {
        MessageBox.Show("The 'DevExpress.XtraPrinting' library is not found", "Error");
        return;
    }

    // Open the Preview window.
    vGrid.ShowPrintPreview();
}

private void PrintVerticalGrid(VGridControl vGrid) {
    // Check whether the Vertical Grid can be printed.
    if (!vGrid.IsPrintingAvailable) {
        MessageBox.Show("The 'DevExpress.XtraPrinting' library is not found", "Error");
        return;
    }

    // Print.
    vGrid.Print();
}
How to: Sort properties using the CustomPropertyDescriptors event

 

5、如何:利用CustomPropertyDescriptors事件排序属性

CodeCentralShow Me

The complete sample project is available in the DevExpress Code Central database at http://www.devexpress.com/example=E2263. Depending on the target platform type (ASP.NET, WinForms, etc), you can either run this example online or download an auto-executable sample.

This example shows how to display an object's properties in a PropertyGridControl in a custom order. Properties are sorted via the CustomPropertyDescriptors event.
使用自定义顺序显示对象属性。
By default, properties are sorted according to their display names. To display properties in a custom order, the properties are sorted, and then the sorted collection is assigned to the event's Properties parameter.

C#:Program.cs
C#:Form1.cs
VB:Form1.vb
VB:Program.vb
 
      
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace PropertySorting {
    static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraVerticalGrid.Events;
using System.Collections;
using System.Globalization;

namespace PropertySorting {
    public partial class Form1 : Form {
        bool allowCustomSorting = false;

        public Form1() {
            InitializeComponent();
            this.propertyGridControl1.SelectedObject = new CustomClass() { Property1 = "one", Property2 = "two", Property3 = "three", Property4 = "four", Property5 = "five" };
        }

        void propertyGridControl1_CustomPropertyDescriptors(object sender, CustomPropertyDescriptorsEventArgs e) {
            if(allowCustomSorting && e.Context.PropertyDescriptor == null) {
                e.Properties = e.Properties.Sort(new string[] { "Property5", "Property4", "Property3", "Property2", "Property1" });
            }
        }
        void button1_Click(object sender, EventArgs e) {
            allowCustomSorting = !allowCustomSorting;
            propertyGridControl1.OptionsBehavior.PropertySort = allowCustomSorting ? DevExpress.XtraVerticalGrid.PropertySort.NoSort : DevExpress.XtraVerticalGrid.PropertySort.Alphabetical;
            propertyGridControl1.Refresh();
            propertyGridControl1.RetrieveFields();
        }
    }
    class CustomClass {
        [DisplayName("C")]
        public string Property1 { get; set; }
        [DisplayName("B")]
        public string Property2 { get; set; }
        [DisplayName("A")]
        public string Property3 { get; set; }
        [DisplayName("D")]
        public string Property4 { get; set; }
        [DisplayName("E")]
        public string Property5 { get; set; }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值