绑定方法,返回列表<员工>到GridView [英] Binding a method that returns List<employee> to a gridview

查看:146
本文介绍了绑定方法,返回列表<员工>到GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的N-层应用程序,返回有方法名单,其中,员工> 。 下面是示例code的方法:

 公开名单<员工> GetAllemployees()
{
    返回DAL.GetEmployees();
}
 

我有我的aspx页面一个GridView。如何设置GridView的数据源为装getEmployees(),让所有的员工都列在GridView的?

解决方案

  myGrid.DataSource = GetAllEmployees();
myGrid.DataBind();
 

有一件事值得一提,你真的想创建一个员工对象,只是检索所有员工?

我会做这样的:

 公共静态列表<员工> 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆