Skip to main content
All docs
V23.2

Use ObjectDataSource

  • 2 minutes to read

ObjectDataSource is a data component much like the standard .NET BindingSource component.

The ObjectDataSource component has the following advantages over BindingSource:

  • It can bind to methods that return a collection of custom objects.
  • It can bind to parameterized object constructors.
  • Since it is a cross-platform component, your project does not need the System.Windows.Forms assembly.

Note

The Object Data Source displays only public non-static properties in the Field List.

Bind to Parameterized Object Constructor

The following code snippet creates the ObjectDataSource component:

DevExpress.DataAccess.ObjectBinding.ObjectDataSource objds = new DevExpress.DataAccess.ObjectBinding.ObjectDataSource();  
objds.Name = "ObjectDataSource1";  
objds.DataSource = typeof(BusinessObject);  
//This line of code passes a parameter value to a parameterized constructor of an underlying data source object.
var parameter = new DevExpress.DataAccess.ObjectBinding.Parameter("p1", typeof(int), 3);  
objds.Constructor = new DevExpress.DataAccess.ObjectBinding.ObjectConstructorInfo(parameter);  
objds.DataMember = "Items";  
...  
public class BusinessObject  {  
    public SampleItems Items { get; set; }  
    public BusinessObject(int p1) {  
        Items = new SampleItems(p1);  
    }  
}  
public class SampleItems : List<SampleItem>  {  
    public SampleItems() : this(10) { }  
    public SampleItems(int itemNumber) {  
        for (int i = 0; i < itemNumber; i++) {  
            Add(new SampleItem() { Name = i.ToString() });  
        }  
    }  
}  
public class SampleItem {  
    public string Name { get; set; }  
}   

Bind to Method that Returns Collection

The following code snippet creates the ObjectDataSource component:

DevExpress.DataAccess.ObjectBinding.ObjectDataSource objds = new DevExpress.DataAccess.ObjectBinding.ObjectDataSource();  
objds.Name = "ObjectDataSource1";  
objds.DataSource = typeof(SampleItem);  
var parameter = new DevExpress.DataAccess.ObjectBinding.Parameter("value", typeof(int), 3);  
objds.Parameters.Add(parameter); 
// Specify all method parameters prior to specifying the method name.
objds.DataMember = "GetData";  
// This line of code is required to obtain the data source object schema. 
objds.Fill();  
...  
 public class SampleItem {  
     public string Name { get; set; }  
     public static List<SampleItem> GetData(int value) {  
         List<SampleItem> items = new List<SampleItem>();  
         for (int i = 0; i < value; i++)  
         {  
             items.Add(new SampleItem() { Name = i.ToString() });  
         }  
         return items;  
     }  
 } 

Parameters

The ObjectDataSource component allows you to specify an external parameter value (Report Parameter) with an Expression-typed parameter.

View Example