Senin, 11 Februari 2008

Object Binding and Search Support

After i followed the video of how to on vbhowto.
i finally realized that an object can be bind ,what most is search,filter support
here is an example of how to add search support, filtered is next


using System.ComponentModel;
namespace SampleObject
{
public class SearchAbleBindingList : BindingList
{
//first override supportsearching core return true
protected override bool SupportsSearchingCore
{
get
{
return true;
}
}
//second override findcore
protected override int FindCore(PropertyDescriptor prop, object key)
{
for(int i=0;i<=Count-1;i++)
{
T item = this[i];
if (prop.GetValue(item).Equals(key)) return i;
}
return -1;
}
}
}

using System.ComponentModel;
namespace SampleObject
{
public class customer
{
private string _name;
private string _adress;

public string name
{
get { return _name; }
set { _name = value; }
}

public string address
{
get { return _adress; }
set { _adress = value; }
}
private BindingList _orders=new BindingList();

public BindingList orders
{
get { return _orders; }
}

public customer(string _name, string _adress)
{
this._name = _name;
this._adress = _adress;
}
}
public class order
{
private int _id;
private string _productname;
private int _quantity;

public int id
{
get { return _id; }
set { _id = value; }
}

public string productname
{
get { return _productname; }
set { _productname = value; }
}

public int quantity
{
get { return _quantity; }
set { _quantity = value; }
}

public order(int _id, string _productname, int _quantity)
{
this._id = _id;
this._productname = _productname;
this._quantity = _quantity;
}
}
}

Tidak ada komentar: