ObjectDataSource, GridView versus Business/Object Model

  • Thread starter Evert Wiesenekker
  • Start date
E

Evert Wiesenekker

I am struggling with the following situation :

Suppose I have an Object model of a house. The house can contain 0 or
more chairs. Lets say the House class looks like this:

class House
{
private List<Chair> _chairs;
public List<Chair> Chairs
{
get
{
return _chairs;
}
set
{
_chairs = value;
}
}
}

When I want to connect this to a GridView I can easily do ('house' is
of course an instance of class 'House'):

theGridView.DataSource = house.Chairs;
theGridView.DataBind();

Works perfect until you set 'AutoGenerateEditButton' to true, you get
an error saying something like 'The GridView fired event RowEditing
which wasn't handled'.

Ok, so I decided to add an ObjectDataSource to my aspx page. I added
the following method to class 'House' (needed for the 'SelectMethod'
property of the ObjectDataSource):

public List<Chairs> GetChairs()
{
return Chairs;
}

The problem is here that the ObjectDataSource instance is creating a
complete new House instance before calling this method which I do not
want because my house instance already exists!

So finally I decided to create a new (static class) with the above
method defined as static.

class ChairsCollection
{
private static List<Chair> _chairs;
public static List<Chair> Chairs
{
get
{
return _chairs;
}
set
{
_chairs = value;
}
}

public static List<Chairs> GetChairs()
{
return Chairs;
}

}

I initialiaze the 'Chairs' property with the one in my house instance
and this works.

Now my question is, is all this work around code needed. Can this be
done simpeler?

Thanks for any help!

Yours sincerely,

Evert Wiesenekker
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top