绑定方法,返回列表&LT;员工&GT;到GridView [英] Binding a method that returns List<employee> to a gridview
问题描述
我在我的N-层应用程序,返回有方法名单,其中,员工&GT;
。
下面是示例code的方法:
公开名单&LT;员工&GT; GetAllemployees()
{
返回DAL.GetEmployees();
}
我有我的aspx页面一个GridView。如何设置GridView的数据源为装getEmployees()
,让所有的员工都列在GridView的?
myGrid.DataSource = GetAllEmployees();
myGrid.DataBind();
有一件事值得一提,你真的想创建一个员工对象,只是检索所有员工?
我会做这样的:
公共静态列表&LT;员工&GT; GetAllEmployees()
{
返回myList中;
}
而在你调用code:
MyGrid.DataSource = EmployeeClass.GetAllEmployees();
MyGrid.DataBind();
在这种方式,你不必实例化一个对象,只是获取对象的列表。
I have method in my N-layered application that returns List<Employee>
.
Below is the sample code for the method:
public List<Employee> GetAllemployees()
{
return DAL.GetEmployees();
}
I have a GridView in my aspx page. How do I set the GridView's datasource as GetEmployees()
so that all the employees are listed in the GridView?
myGrid.DataSource = GetAllEmployees();
myGrid.DataBind();
One thing worth mentioning, do you really want to create an employee object just to retrieve all employees?
I would do it like this:
public static List<Employee> GetAllEmployees()
{
return myList;
}
And in your calling code:
MyGrid.DataSource = EmployeeClass.GetAllEmployees();
MyGrid.DataBind();
In this way you do not have to instantiate an object that simply gets a list of the object.
这篇关于绑定方法,返回列表&LT;员工&GT;到GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!