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
- UnboundRow.cs (VB: UnboundRow.vb)
- UnboundRowHelper.cs (VB: UnboundRowHelper.vb)
- Form1.cs (VB: Form1.vb)
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
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;
}
}
}
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);
}
}
}
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; }
}
}