Introduction
If you read MSDN you will be hard pressed to find out how to bind a business object to a datagrid. I thought to make up for the lack of business object documentation; I would write a simple tutorial on how to bind an object to a datagrid.
First Create Your Class
First let’s create a normal class nothing fancy:
using System;
namespace Employee
{
public class Employee
{
public Employee()
{
}
#region Employee Accessors
private string _firstName;
private string _lastName;
private int _employeeNumber;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
}
}
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
}
}
public int EmployeeNumber
{
get
{
return _employeeNumber;
}
set
{
_employeeNumber = value;
}
}
#endregion
}
}
Create a collection of your Business Object
The MSDN documentation doesn't give you a concrete example of binding using a business object but they do say that a datagrid is able to bind to anything that implements the IList (single dimension arrays implement the IList interface), IListSource, or IBindingList interfaces.
Reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskbindingthedatagridcontroltoanadodataset.asp
So we will make a collection class of Employee objects that implement the IList interface:
using System;
using System.Collections;
namespace Employee
{
public class EmployeeCollection : CollectionBase
{
public Employee this[int index]
{
get
{
return (Employee)List[index];
}
set
{
List[index] = value;
}
}
public int Add(Employee value)
{
return (List.Add(value));
}
public int IndexOf(Employee value)
{
return (List.IndexOf(value));
}
public void Insert(int index, Employee value)
{
List.Insert(index, value);
}
public void Remove(Employee value)
{
List.Remove(value);
}
public bool Contains(Employee value)
{
return (List.Contains(value));
}
}
}
Binding to the Employee collection
Now you can use the collection and bind to the grid. Using a windows form, here is the code to add an employee to the grid and also to the employee collection:
private void btnAddToGrid_Click(object sender, System.EventArgs e)
{
Employee Employee = new Employee();
Employee.FirstName = txtFirstName.Text;
Employee.LastName = txtLastName.Text;
Employee.EmployeeNumber = Convert.ToInt32(txtEmployeeNumber.Text);
EmployeeCollection.Add(Employee);
//Bind to grid here.
dataGrid1.DataSource = EmployeeCollection;
CurrencyManager cm =
(CurrencyManager) this.dataGrid1.BindingContext[EmployeeCollection];
if (cm != null)
{
cm.Refresh();
}
}
Here is the code to remove the employee object from the grid and also from the employee collection:
private void btnRemoveFromGrid_Click(object sender, System.EventArgs e)
{ EmployeeCollection.RemoveAt(dataGrid1.CurrentCell.RowNumber);
//Bind to grid here.
dataGrid1.DataSource = EmployeeCollection;
CurrencyManager cm =
(CurrencyManager) this.dataGrid1.BindingContext[EmployeeCollection];
if (cm != null)
{
cm.Refresh();
}
}
A good article explaining the pros and cons of datasets can be found at:
http://msdn.microsoft.com/asp.net/default.aspx?pull=/library/en-us/dnaspp/html/CustEntCls.asp
Also the book Expert C# Business Objects goes into more depth than my code example and is a great read.
http://www.lhotka.net