Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

DataGrid

I have a simple question, I have a collection (using CollectionBase) that I wish to display in a Datagrid.

To this end I am putting a datagrid on a form and running the code:

grid.DataSource = MyCollection.

since CollectionBase inherits IList I expect my grid to magically populate with the contents of my collection, alas, this does not occur and I get a grid with the same number of rows as the collection count but no collection attributes visible.

Is there more to it than this?
Typically I have used collections and objects over the last few years to say, populate a grid, and then the user selects a row in the grid and gets a particular object based on the collection key specified in the collection key column of the grid. Is this still a good way to go with .NET or are more people populating datagrids with ADO recordsets as the databinding is better than it used to be?

Any pointers or references to information gratefully received.

Cheers

Realist
Tuesday, June 17, 2003

What is stored in the collection? If it is your own custom class then it needs to have Property procedures, ie:

C#:
string _name;
public string Name
{
  get { return _name; }
  set { _name = value; }
}

VB.NET:
Private _name As String

Public Property Name() As String
  Get
      Return _name
  End Get
  Set(ByVal value As String)
      _name = value
  End Set
End Property

The DataGrid only "reflects" the Properties not the Fields of the class.

Duncan Smart
Tuesday, June 17, 2003

yes my collection/class contains all these properties and functions perfectly Ie I can iterate through the collection printing out all attributes, i.e Customer.Name.

Realist
Tuesday, June 17, 2003

Ok - I worked it out, all my properties in my Object class were defined as 'friend', I changed them all to public and it all worked as expected. I don't really get this as 'friend' should be project scoped. Anyway, it works and I'll build upon that.

Cheers,

Realist
Tuesday, June 17, 2003

"Friend" does not give public visibility to the property. The code that's reflecting your object lives OUTSIDE your assembly (in the ASP.NET assemblies).

Brad Wilson (dotnetguy.techieswithcats.com)
Tuesday, June 17, 2003

Ahhh - I get it thanks.

Realist
Tuesday, June 17, 2003

*  Recent Topics

*  Fog Creek Home