Example E3004
Visible to All Users

WinForms Property Grid - How to implement unbound rows

The WinForms Property Grid Control does not support unbound rows out of the box. This example demonstrates how to implement this functionality using custom property descriptors. See the implementation of the CustomPropertyDescriptors event handler:

C#
// Stores property values that are not present in the selected object. void _PropertyGrid_CustomPropertyDescriptors(object sender, CustomPropertyDescriptorsEventArgs e) { if ((sender as PropertyGridControl).SelectedObject == e.Source) { PropertyDescriptorCollection properties = e.Properties; ArrayList list = new ArrayList(properties); list.AddRange(_UnboundRows); PropertyDescriptor[] result = new PropertyDescriptor[list.Count]; list.ToArray().CopyTo(result, 0); e.Properties = new PropertyDescriptorCollection(result); } }

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

DXApplication3/CustomDescriptor/UnboundRow.cs(vb)
C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; namespace DXApplication3 { public class UnboundRow : PropertyDescriptor { private object _MyValue; private readonly Type _ValueType; public object MyValue { get { return _MyValue; } set { _MyValue = value; } } public UnboundRow(string name, Type valueType) : base(name, null) { _ValueType = valueType; } protected UnboundRow(string name, Attribute[] attrs) : base(name, attrs) { } protected UnboundRow(MemberDescriptor descr) : base(descr) { } protected UnboundRow(MemberDescriptor descr, Attribute[] attrs) : base(descr, attrs) { } public override string Category { get { return "!Unbound rows"; } } public override bool CanResetValue(object component) { return false; } public override Type ComponentType { get { return _ValueType; } } public override object GetValue(object component) { return MyValue; } public override bool IsReadOnly { get { return false; } } public override Type PropertyType { get { return _ValueType; } } public override void ResetValue(object component) { } public override void SetValue(object component, object value) { MyValue = value; } public override bool ShouldSerializeValue(object component) { return false; } } }
DXApplication3/CustomDescriptor/UnboundRowHelper.cs(vb)
C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DevExpress.XtraVerticalGrid; using DevExpress.XtraVerticalGrid.Events; using System.ComponentModel; using System.Collections; using DevExpress.XtraVerticalGrid.Rows; namespace DXApplication3 { public class UnboundRowsHelper { private readonly PropertyGridControl _PropertyGrid; private List<UnboundRow> _UnboundRows = new List<UnboundRow>(); public UnboundRowsHelper(PropertyGridControl pg) { _PropertyGrid = pg; _PropertyGrid.CustomPropertyDescriptors += _PropertyGrid_CustomPropertyDescriptors; } void _PropertyGrid_CustomPropertyDescriptors(object sender, CustomPropertyDescriptorsEventArgs e) { if ((sender as PropertyGridControl).SelectedObject == e.Source) { PropertyDescriptorCollection properties = e.Properties; ArrayList list = new ArrayList(properties); list.AddRange(_UnboundRows); PropertyDescriptor[] result = new PropertyDescriptor[list.Count]; list.ToArray().CopyTo(result, 0); e.Properties = new PropertyDescriptorCollection(result); } } public void AddUnboundRow(string fieldName, Type valueType) { UnboundRow row = new UnboundRow(fieldName, valueType); _UnboundRows.Add(row); } } }
DXApplication3/Form1.cs(vb)
C#
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; namespace DXApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); UnboundRowsHelper helper = new UnboundRowsHelper(propertyGridControl1); helper.AddUnboundRow("UnboundInt", typeof(int)); helper.AddUnboundRow("UnboundString", typeof(string)); helper.AddUnboundRow("UnboundDateTime", typeof(DateTime)); propertyGridControl1.RetrieveFields(); } } public class SourceObject : Component { public string TestProperty { get; set; } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.