tags:

views:

33

answers:

2

Hello, I have method in my N-Layered application that returns List. Below is the sample code for the method

public List<employee> GetAllemployees()
{
  List<employee> empl =new List<employee>

    empl=DAL.GetEmployees();

return empl;
} 

Now I have a gridview in my aspx page and how do I set the gridview datasource as GetEmployees() so that all the employees are listed in the GridView. Thanks

+4  A: 

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!

Mitchel Sellers
Err beat me by less then 30 seconds...
JonH
He's assuming a static method `GetAllEmployees()` of a class called `MyClass`
JonH
+1  A: 
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()
 {
  //get the data
  return myList;
 }

And in your calling code:

List<employee> l;
l = EmployeeClass.GetAllEmployees();
MyGrid.DataSource = l;
MyGrid.DataBind();

In this way you do not have to instantiate an object that simply gets a list of the object.

JonH