corelibraries-403658-devexpress-data-library-data-sources-use-object-data-source.md
ObjectDataSource is a data component much like the standard .NET BindingSource component.
The ObjectDataSource component has the following advantages over BindingSource :
Note
The Object Data Source displays only public non-static properties in the Field List.
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; }
}
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;
}
}
The ObjectDataSource component allows you to specify an external parameter value (Report Parameter) with an Expression-typed parameter.