Windows Develop Bookmark and Share   
 index > Windows Forms Designer > PropertyGrid with a Generic Collection
 

PropertyGrid with a Generic Collection

Hi

Should I be able to use a simple Collection in a PropertyGrid, without all the code described in Gerd Klevesaat's article on Code Project?

http://www.codeproject.com/KB/tabs/customizingcollectiondata.aspx?fid=16073&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=1#xx0xx

I have used a PropertyGrid to display an instance and it appears to work but ....

What I have is:

- Class Foo, with three String Properties, and one Boolean ReadOnly Property

- Class FooCollection which inherits Collections.Generic.List(Of Foo)

(That's all, there is noother Code in FooCollection)

- Class FooBar, which has

Property Title as string

Property Foos as FooCollection

- In my Form the PropertyGrid.SelectedObject is an instance of FooBar

- I have used some of the ComponentModel Attribute tags (like CategoryAttribute) as described in Gerd's earlier article

http://www.codeproject.com/KB/vb/using_propertygrid.aspx

What happens is:

- The FooBar PropertyGrid shows OK, I see Title and Foos properties

- If I change the Title value it is updated in the FooBar instance

- I see theFoos property and click on the Colection Editoor button

- The Collection Editor dialogue shows

- Click on the Add button and an instance of Foo is added (in the left panel)

- I see all the Foo properties on the Right panel and can change them. Values are returned to the Foo instance.

- I can continue adding new Foo instances to the collection.

When I click OK on the Collection Editor the FooCollection is NOT returned to the Foos Property. If I click on the Collection Editor button in Foos again, the Collection Editor is empty.

Do I have to do all the customisation described in Gerd's article in order to get this simple Collection to work in the PropertyGrid, or is there something simpler that I've missed?

VS 2008, .NET 3.5, VB

Thanks

Steve

Sedge.Rev  Wednesday, November 26, 2008 9:52 PM

Hi Sedge.Rev,

I don’t think you need to write all of the code as the article you refer to. The only thing I am not very sure is if you question is a design time issue or a run time issue.

If the problem occurs at design time, please try to add DesignerSerializationAttribute for the collection Property. I have commented this in the following code. Otherwise, if it ocurrs at runtime, the attribute is not necessary.

Code Snippet

public Form2()

{

InitializeComponent();

FooBar f = new FooBar();

this.propertyGrid1.SelectedObject = f;

}

class Foo

{

string aa;

public string Aa

{

get { return aa; }

set { aa = value; }

}

string bb;

public string Bb

{

get { return bb; }

set { bb = value; }

}

readonly bool flag;

public bool Flag

{

get { return flag; }

}

}

class FooBar

{

List<Foo> a = new List<Foo>();

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]// if the problem occured at design time,try to add this attribute.

public List<Foo> A

{

get { return a; }

set { a = value; }

}

string title;

public string Title

{

get { return title; }

set { title = value; }

}

}

}

If you have further problems, please feel free to let me know.

Best regards,

Bruce Zhou

Bruce.Zhou  Tuesday, December 02, 2008 10:35 AM

Hi Sedge.Rev,

I don’t think you need to write all of the code as the article you refer to. The only thing I am not very sure is if you question is a design time issue or a run time issue.

If the problem occurs at design time, please try to add DesignerSerializationAttribute for the collection Property. I have commented this in the following code. Otherwise, if it ocurrs at runtime, the attribute is not necessary.

Code Snippet

public Form2()

{

InitializeComponent();

FooBar f = new FooBar();

this.propertyGrid1.SelectedObject = f;

}

class Foo

{

string aa;

public string Aa

{

get { return aa; }

set { aa = value; }

}

string bb;

public string Bb

{

get { return bb; }

set { bb = value; }

}

readonly bool flag;

public bool Flag

{

get { return flag; }

}

}

class FooBar

{

List<Foo> a = new List<Foo>();

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]// if the problem occured at design time,try to add this attribute.

public List<Foo> A

{

get { return a; }

set { a = value; }

}

string title;

public string Title

{

get { return title; }

set { title = value; }

}

}

}

If you have further problems, please feel free to let me know.

Best regards,

Bruce Zhou

Bruce.Zhou  Tuesday, December 02, 2008 10:35 AM
Hi Bruce,

I have the same problem as the original poster. I can edit the property with the list using the collection editor but the changes are never saved to the original property so my changes disappear. This happens at runtime.

Any tips on how i might get this to work?

Regards

urk_forever  Friday, February 27, 2009 1:19 PM

You can use google to search for other answers

Custom Search

More Threads

• Form CancelButton, AcceptButton problem
• Create custom UI type editor for control property ?
• VS2005 Simple custom control painting
• Ann: SharpLibrary Released
• Using WMI to get date and time on a remote computer
• UITypeEditor for complex type
• DataGridView In DesignSurface
• how can i use logical expression
• DataGridViewColumn
• time on a form