Note that there are some explanatory texts on larger screens.

plurals
  1. POcustom collection in property grid
    text
    copied!<p>I'm using this article as a reference to use custom collection in propertygrid: <a href="http://www.codeproject.com/KB/tabs/customizingcollectiondata.aspx?fid=16073&amp;df=90&amp;mpp=25&amp;noise=3&amp;sort=Position&amp;view=Quick&amp;fr=1#xx0xx" rel="nofollow">LINK</a> </p> <p>When I open the collectioneditor and remove all items then I press OK, I get an exception if null.</p> <p>How can i solve that ?<br> I am using:</p> <pre><code>public T this[int index] { get { if (List.Count == 0) { return default(T); } else { return (T)this.List[index]; } } } </code></pre> <p>as a getter for an item, of course if I have no object how can i restart the whole collection ?</p> <p>this is the whole code</p> <pre><code>/// &lt;summary&gt; /// A generic folder settings collection to use in a property grid. /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;can be import or export folder settings.&lt;/typeparam&gt; [Serializable] [TypeConverter(typeof(FolderSettingsCollectionConverter)), Editor(typeof(FolderSettingsCollectionEditor), typeof(UITypeEditor))] public class FolderSettingsCollection_New&lt;T&gt; : CollectionBase, ICustomTypeDescriptor { private bool m_bRestrictNumberOfItems; private int m_bNumberOfItems; private Dictionary&lt;string, int&gt; m_UID2Idx = new Dictionary&lt;string, int&gt;(); private T[] arrTmp; /// &lt;summary&gt; /// C'tor, can determine the number of objects to hold. /// &lt;/summary&gt; /// &lt;param name="bRestrictNumberOfItems"&gt;restrict the number of folders to hold.&lt;/param&gt; /// &lt;param name="iNumberOfItems"&gt;The number of folders to hold.&lt;/param&gt; public FolderSettingsCollection_New(bool bRestrictNumberOfItems = false , int iNumberOfItems = 1) { m_bRestrictNumberOfItems = bRestrictNumberOfItems; m_bNumberOfItems = iNumberOfItems; } /// &lt;summary&gt; /// Add folder to collection. /// &lt;/summary&gt; /// &lt;param name="t"&gt;Folder to add.&lt;/param&gt; public void Add(T t) { if (m_bRestrictNumberOfItems) { if (this.List.Count &gt;= m_bNumberOfItems) { return; } } int index = this.List.Add(t); if (t is WriteDataFolderSettings || t is ReadDataFolderSettings) { FolderSettingsBase tmp = t as FolderSettingsBase; m_UID2Idx.Add(tmp.UID, index); } } /// &lt;summary&gt; /// Remove folder to collection. /// &lt;/summary&gt; /// &lt;param name="t"&gt;Folder to remove.&lt;/param&gt; public void Remove(T t) { this.List.Remove(t); if (t is WriteDataFolderSettings || t is ReadDataFolderSettings) { FolderSettingsBase tmp = t as FolderSettingsBase; m_UID2Idx.Remove(tmp.UID); } } /// &lt;summary&gt; /// Gets ot sets a folder. /// &lt;/summary&gt; /// &lt;param name="index"&gt;The index of the folder in the collection.&lt;/param&gt; /// &lt;returns&gt;A folder object.&lt;/returns&gt; public T this[int index] { get { //if (List.Count == 0) //{ // return default(T); //} //else //{ return (T)this.List[index]; //} } } /// &lt;summary&gt; /// Gets or sets a folder. /// &lt;/summary&gt; /// &lt;param name="sUID"&gt;The UID of the folder.&lt;/param&gt; /// &lt;returns&gt;A folder object.&lt;/returns&gt; public T this[string sUID] { get { if (this.Count == 0 || !m_UID2Idx.ContainsKey(sUID)) { return default(T); } else { return (T)this.List[m_UID2Idx[sUID]]; } } } /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;param name="sUID"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public bool ContainsItemByUID(string sUID) { return m_UID2Idx.ContainsKey(sUID); } /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; public String GetClassName() { return TypeDescriptor.GetClassName(this, true); } /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; public AttributeCollection GetAttributes() { return TypeDescriptor.GetAttributes(this, true); } /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; public String GetComponentName() { return TypeDescriptor.GetComponentName(this, true); } /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; public TypeConverter GetConverter() { return TypeDescriptor.GetConverter(this, true); } /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; public EventDescriptor GetDefaultEvent() { return TypeDescriptor.GetDefaultEvent(this, true); } /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; public PropertyDescriptor GetDefaultProperty() { return TypeDescriptor.GetDefaultProperty(this, true); } /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;param name="editorBaseType"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public object GetEditor(Type editorBaseType) { return TypeDescriptor.GetEditor(this, editorBaseType, true); } /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;param name="attributes"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public EventDescriptorCollection GetEvents(Attribute[] attributes) { return TypeDescriptor.GetEvents(this, attributes, true); } /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; public EventDescriptorCollection GetEvents() { return TypeDescriptor.GetEvents(this, true); } /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;param name="pd"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public object GetPropertyOwner(PropertyDescriptor pd) { return this; } /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;param name="attributes"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public PropertyDescriptorCollection GetProperties(Attribute[] attributes) { return GetProperties(); } /// &lt;summary&gt; /// Called to get the properties of this type. /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; public PropertyDescriptorCollection GetProperties() { // Create a collection object to hold property descriptors PropertyDescriptorCollection pds = new PropertyDescriptorCollection(null); // Iterate the list of employees for (int i = 0; i &lt; this.List.Count; i++) { // Create a property descriptor for the employee item and add to the property descriptor collection CollectionPropertyDescriptor_New&lt;T&gt; pd = new CollectionPropertyDescriptor_New&lt;T&gt;(this, i); pds.Add(pd); } // return the property descriptor collection return pds; } public T[] ToArray() { if (arrTmp == null) { arrTmp = new T[List.Count]; for (int i = 0; i &lt; List.Count; i++) { arrTmp[i] = (T)List[i]; } } return arrTmp; } } /// &lt;summary&gt; /// Enable to display data about a collection in a property grid. /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;Folder object.&lt;/typeparam&gt; public class CollectionPropertyDescriptor_New&lt;T&gt; : PropertyDescriptor { private FolderSettingsCollection_New&lt;T&gt; collection = null; private int index = -1; /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;param name="coll"&gt;&lt;/param&gt; /// &lt;param name="idx"&gt;&lt;/param&gt; public CollectionPropertyDescriptor_New(FolderSettingsCollection_New&lt;T&gt; coll, int idx) : base("#" + idx.ToString(), null) { this.collection = coll; this.index = idx; } /// &lt;summary&gt; /// /// &lt;/summary&gt; public override AttributeCollection Attributes { get { return new AttributeCollection(null); } } /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;param name="component"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public override bool CanResetValue(object component) { return true; } /// &lt;summary&gt; /// /// &lt;/summary&gt; public override Type ComponentType { get { return this.collection.GetType(); } } /// &lt;summary&gt; /// /// &lt;/summary&gt; public override string DisplayName { get { if (this.collection[index] != null) { return this.collection[index].ToString(); } else { return null; } } } public override string Description { get { return ""; } } /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;param name="component"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public override object GetValue(object component) { if (this.collection[index] != null) { return this.collection[index]; } else { return null; } } /// &lt;summary&gt; /// /// &lt;/summary&gt; public override bool IsReadOnly { get { return false; } } public override string Name { get { return "#" + index.ToString(); } } /// &lt;summary&gt; /// /// &lt;/summary&gt; public override Type PropertyType { get { return this.collection[index].GetType(); } } public override void ResetValue(object component) { } /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;param name="component"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public override bool ShouldSerializeValue(object component) { return true; } /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;param name="component"&gt;&lt;/param&gt; /// &lt;param name="value"&gt;&lt;/param&gt; public override void SetValue(object component, object value) { // this.collection[index] = value; } } </code></pre> <hr> <p>Well guys thank you all for help.</p> <p>i finally won the fight.</p> <p>the Display name returned null when (index = 0) == null.</p> <p>instead of String.Empty ... </p> <p>hope this will help some of you</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload