开发者

Binding a method that returns List<employee> to a gridview

I have method in my N-layered application that returns 开发者_StackOverflow中文版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.


Just like any other bindings, the result of the method call is the datasource, then call "DataBind". My example below assumes an instance of your class that contains the "GetAllEmployees" method that is called MyClass.

  GridView1.DataSource = myInstance.GetAllEmployees();
  GridView1.DataBind();

That is it!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜