IEnumerable<T> can be used as datasource for some controls especially on collection thing like gridview or else. It happens when assigning Datasource property from the control with data in it.
for example:
IEnumerable<Sales> sales = GetAllSales(); // return value from GetSales is IEnumerable<Sales>
GridView1.DataSource = sales;
GridView1.DataBind();
The samples above will display all sales from methods. This is good when there is no paging in GridView. For advanced use like paging or else; Try to cast or convert to BindingList<T> so it can be use in the control.
public static BindingList<T> ToBindingList<T>(this IEnumerable<T> data)
{
BindingList<T> result = null;
if (data != null)
{
result = new BindingList<T>();
foreach (T item in data)
{
result.Add(item);
}
}
return result;
}
The method above will help to convert to BindingList<T> and ready to use as datasource in control
1 comments:
Thanks! Solved the problem.
Post a Comment