Link to home
Create AccountLog in
Avatar of csjordan

asked on

How do I bind generic collection fields (not properties) to a datagrid?

I am having an issue binding my collection directly to a datagridview object.  For some reason, only the properties of my class display on the datagridview, and the fields are missing.  I have tried using column autogeneration and I have also tried adding and binding columns manually, but neither method appears to be working for displaying the information.

I'd prefer not to have to convert all my class fields to properties.  I am using 2008.  Any suggestions?
Avatar of chrissie1
Flag of Belgium image

I don't think you can do that with fields.

Are they public fields?
I don't think you can either. If you want to do this you can create a presentation class to bind to the grid. Simply put properties into the presentation class that have the right naming, and pipe those properties to the fields on your other class. You can easily do this by inheriting from the main class.
The only values that are going to show up are the public fields and properties of your class being displayed in the DataGridView...  this makes sense if you think about it because the DataGridView control is just a control like everything else; it does not have access to non public fields.
Avatar of csjordan


Yes the fields are public, which is why I expected them to display.  

Zach: I'm not sure I understand what you mean by a presentation class.  I'm a liberal arts major masquerading as a programmer here, so you are probably going to need to use really small words and talk real slow for me :).

I know how to inherit objects but I'm not sure what you mean by creating a presentation class.
Ok, here's what I mean:
public class MyMainClass
     protected string myString = "here it is";
public class MyPresentationClass : MyMainClass
     public string MyString
          get { return myString; }

Open in new window

Avatar of ZachSmith
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Ok, I though that was what you meant, basically creating a wrapper and then providing access via the property's get/set methods that read/write to the base class fields.

The short reason I don't want to set them up as properties is because it is too much typing to create the local copies of the variables and the get and set methods for each property.  I am lazy and I figured that fields were treated exactly as properties, without the explicit get/set and local variables.

I guess I don't understand the advantages of encapsulating via the property in lieu of making the field public in scope.

Thanks for your reply.  I'm going to accept the solution, but if you have the time, i'd appreciate an explanation of the encapsulation benefits.
BTW - I already know about the advantages of being able to manipulate the values or execute other methods  before they are returned or set.

I guess I am thinking along the lines of performance/security.
If you type "prop" and then hit tab, Visual Studio will automatically put in the get/set and property structure. You simply have to hit tab a couple more times to tell it what to name the property and the property's attributes.