Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying a collection returned from ObjectDataSource

Some quick background on what I'm working on:

  • I've got a page with a RadGrid tied to an ObjectDataSource that returns a collection of objects
  • On that page; I also have two lists; one contains Ids of objects that need to merged with the collection returned from the ObjectDataSource, and the other contains Ids of objects that need to be removed from that collection
  • The current process for handling this is very hack-ish; when an item is added to either one of those two lists; a separate method is called which dumps the results of myDataSource.Select() into a variable, and performs the manipulation. The grid then has its DataSourceId cleared, and the collection is assigned to the DataSource property, and then DataBind is called. (essentially untying the ObjectDataSource and using manual databinding instead)

My question is as follows: Is it possible to somehow "intercept" the collection returned from the ObjectDataSource so I can manipulate the collection before it is passed to the RadGrid?

If this isn't possible; it's not a dealbreaker. I believe I can modify the Select() method to accept the two lists; and perform the manipulation there. Another avenue I have is to implement the NeedDataSource() event for the grid, and manipulate there as well.

Ideally, I'd like to use the first option. Has anybody been successful doing this before?

like image 333
Jim B Avatar asked Nov 05 '22 05:11

Jim B


1 Answers

You can derive from ObjectDataSource & ObjectDataSourceView, and respectively override GetView & ExecuteSelect, something like this:

public class MyObjectDataSource : ObjectDataSource
{
    private MyObjectDataSourceView _view;
    private MyObjectDataSourceView GetView()
    {
        if (_view == null)
        {
            _view = new MyObjectDataSourceView(this, "DefaultView", Context);
            if (IsTrackingViewState)
            {
                ((IStateManager)_view).TrackViewState();
            }
        }
        return _view;
    }

    protected override DataSourceView GetView(string viewName)
    {
        return GetView();
    }
}

public class MyObjectDataSourceView : ObjectDataSourceView
{
    public MyObjectDataSourceView(MyObjectDataSource owner, string name, HttpContext context)
        : base(owner, name, context)
    {
    }

    protected override IEnumerable ExecuteSelect(DataSourceSelectArguments arguments)
    {
        IEnumerable dataSource = base.ExecuteSelect(arguments);
        // TODO: do your stuff here
        return dataSource;
    }
}
like image 145
Simon Mourier Avatar answered Nov 11 '22 06:11

Simon Mourier