Introduce DataBase,Asp.net,JavaScript,Xml,Html,Css,Sql,Php,ASP.NET Controls,AJAX,Tools,HTML,CSS,JavaScript,Open Source Project,WPF,.Net Framework,Linq
Top Recommended Hosting

Data Objects and Collections

by the3factory 5/4/2008 7:01:00 AM

Data sources in a Windows Forms application are composed of object instances. A data source may be a single instance of a single object, or it may be a collection of object instances, where the collection itself is an instance of a container object. Those instances may be of almost any type, including types in the .NET Framework or types that you create as class or structure definitions. As a result, discussing data binding can get a little confusing, because you have to talk in generalized terms about the things that you are binding to. They may be a DataSet, an ArrayList, a BindingList<Customer>, a Customer, a Foo, or some unknown type that you program against through an interface reference without knowing the actual object type. Additionally, even once you get down to the individual object level, you need to describe the parts of that object that you are using for data binding, and those may be properties, fields, variables, or columns.

Throughout this book, I refer to an object as a data item if it's contained within some sort of collection that is being used for data binding. That may be an instance of a Customer class within a BindingList<Customer> collection, or it may be a DataRow within a DataTable within a DataSet. I refer to collections of objects as a collection or a list. However, to be used for many data-binding scenarios, a collection has to implement the IList interface to be properly described as a list.

The part of a data item being bound to could be a public property on an object instance, or it could be a column within a data row (also sometimes referred to as a field). For these situations, I use the term property, and if the collection is a data table, you can translate this to mean "the column within the data row that is the current data item."

You can think of a data member as a relative path to some piece of information within an object that contains data. If the object is a single instance of a data item, then the data member may just be the name of the property on the object that you want to use for data binding. For example, when you set up data binding to a TextBox control , you create an instance of the Binding class. The second parameter to the constructor is the data source, and the third parameter is the data member. The following example shows how to pass a reference to an instance of a Customer object as the data source and pass "CompanyName" as the data member:

Customer cust = new Customer();
cust.CompanyName = "Northwind Traders";
Binding bind = new Binding("Text", cust, "CompanyName", true);
m_TextBox.DataBindings.Add(bind);

If the object itself is a container of multiple collections of objects, such as a DataSet that contains more than one DataTable, or is a custom object that has a property that exposes a child collection of objects, then the data member is the name of the property exposed from that container object. For example, when you set up data binding for a BindingSource component,  to a complex object hierarchy, you can set the DataSource property to the top-level data container object, and use the DataMember property to resolve the part of that container that holds the list of data you are binding to. For example:

public class Order
{
public int OrderId { ... }
public DateTime OrderDate { ... }
public BindingList<OrderDetail> Details { ... }
}
public class Customer
{   public BindingList<T> Orders { ... }
}
public class MyForm : Form
{
private BindingSource m_BindingSource = new BindingSource();
Customer m_Customer = new Customer();
private void OnFormLoad(object sender, EventArgs args)
{
m_BindingSource.DataSource = m_Customer;
m_BindingSource.DataMember = "Orders";
Binding textBoxBinding =
new Binding("Text",m_BindingSource, "OrderDate",true);
}
}

In this example, Orders is the relative path within the Customer object to the list that the BindingSource component is bound to. The data source and data member for that binding source represents a list collection itself. That binding source is then used as the data source for a Binding object, specifying the data member as OrderDatea path within the current item in the data source list.
 

Related posts

Sign up for PayPal and start accepting credit card payments instantly.


Powered by BlogEngine.NET 1.2.0.0