namespace ITyped.Models
{
[Serializable()]
public class SortableBindingList<T> : BindingList<T>, ITypedList
{
[NonSerialized()]
private PropertyDescriptorCollection properties;
public SortableBindingList()
: base()
{
// Get the 'shape' of the list.
// Only get the public properties marked with Browsable = true.
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(
typeof(T),
new Attribute[] { new BrowsableAttribute(true) });
// Sort the properties.
properties = pdc.Sort();
}
#region ITypedList Implementation
public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
{
var propList = new List<PropertyDescriptor>
{
new MyPropDesc("My prop !", null)
};
return new PropertyDescriptorCollection(propList.ToArray());
}
// This method is only used in the design-time framework
// and by the obsolete DataGrid control.
public string GetListName(PropertyDescriptor[] listAccessors)
{
return typeof(T).Name;
}
private class MyPropDesc : PropertyDescriptor
{
public MyPropDesc(string name, Attribute[] attrs) : base(name, attrs)
{
}
public MyPropDesc(MemberDescriptor descr) : base(descr)
{
}
public MyPropDesc(MemberDescriptor descr, Attribute[] attrs) : base(descr, attrs)
{
}
public override bool CanResetValue(object component)
{
return false;
}
public override object GetValue(object component)
{
var s = (Student) component;
return s.Title + " uc: " + s.UniversityCode;
}
public override void ResetValue(object component)
{
}
public override void SetValue(object component, object value)
{
}
public override bool ShouldSerializeValue(object component)
{
return false;
}
public override Type ComponentType
{
get { return typeof(Student); }
}
public override bool IsReadOnly
{
get { return true; }
}
public override Type PropertyType
{
get { return typeof(string); }
}
}
#endregion
}
public class Student
{
public Nullable<Int32> UniversityCode { get; set; }
public string Title { get; set; }
public Nullable<Int32> Duration { get; set; }
public double CourseFees { get; set; }
public double CGPA { get; set; }
}
public class StudentDetailsDataContext
{
public SortableBindingList<Student> GetStudent
{
get
{
int code = 10000;
var data = new SortableBindingList<Student> { };
for (long i = 0; i < 1000; i++)
{
Student[] s = new Student[10];
s[0] = new Student() { UniversityCode = code + 1, CourseFees = 2000.00, CGPA = 7.52, Duration = 90, Title = "Distributed Component Architecture" };
s[1] = new Student() { UniversityCode = code + 2, CourseFees = 1000.00, CGPA = 9.55, Duration = 60, Title = "Data Structures" };
s[2] = new Student() { UniversityCode = code + 3, CourseFees = 1750.00, CGPA = 9.03, Duration = 75, Title = "Neural Networks" };
s[3] = new Student() { UniversityCode = code + 4, CourseFees = 2000.00, CGPA = 8.91, Duration = 90, Title = "Genetic Algorithms" };
s[4] = new Student() { UniversityCode = code + 5, CourseFees = 1000.00, CGPA = 9.55, Duration = 30, Title = "Grid Computing" };
s[5] = new Student() { UniversityCode = code + 6, CourseFees = 2500.00, CGPA = 9.87, Duration = 60, Title = "Cloud Computing" };
s[6] = new Student() { UniversityCode = code + 7, CourseFees = 1500.00, CGPA = 9.75, Duration = 90, Title = "Enterprise Computing" };
s[7] = new Student() { UniversityCode = code + 8, CourseFees = 1250.00, CGPA = 9.66, Duration = 45, Title = "Mobile Computing" };
s[8] = new Student() { UniversityCode = code + 9, CourseFees = 1000.00, CGPA = 8.33, Duration = 60, Title = "WAP and XML" };
s[9] = new Student() { UniversityCode = code + 10, CourseFees = 1500.00, CGPA = 8.66, Duration = 75, Title = "Design Patterns" };
foreach (Student studnt in s)
{
data.Add(studnt);
}
code += 10;
}
return data;
}
}
}
}