使用 DAL BLL 函数的 Windows 窗体不显示对象
Posted
技术标签:
【中文标题】使用 DAL BLL 函数的 Windows 窗体不显示对象【英文标题】:Windows form using DAL BLL functions are not show for an object 【发布时间】:2013-02-23 09:59:28 【问题描述】:我的 EmployeeDB 类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Test
public class EmployeeDB
private string connectionString;
public EmployeeDB()
//my connectionstring info
public int CountEmployees()
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("CountEmployees", con);
cmd.CommandType = CommandType.StoredProcedure;
try
con.Open();
return (int)cmd.ExecuteScalar();
catch (SqlException err)
// Replace the error with something less specific.
// You could also log the error now.
throw new ApplicationException("Data error.");
finally
con.Close();
public List<EmployeeDetails> GetEmployees()
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("GetAllEmployees", con);
cmd.CommandType = CommandType.StoredProcedure;
// Create a collection for all the employee records.
List<EmployeeDetails> employees = new List<EmployeeDetails>();
try
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
EmployeeDetails emp = new EmployeeDetails(
(int)reader["EmployeeID"], (string)reader["FirstName"],
(string)reader["LastName"], (string)reader["TitleOfCourtesy"]);
employees.Add(emp);
reader.Close();
return employees;
catch (SqlException err)
// Replace the error with something less specific.
// You could also log the error now.
throw new ApplicationException("Data error.");
finally
con.Close();
我的员工详细信息类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace Test
public class EmployeeDetails
private int employeeID;
private string firstName;
private string lastName;
private string titleOfCourtesy;
public int EmployeeID
get return employeeID;
set employeeID = value;
public string FirstName
get return firstName;
set firstName = value;
public string LastName
get return lastName;
set lastName = value;
public string TitleOfCourtesy
get return titleOfCourtesy;
set titleOfCourtesy = value;
public EmployeeDetails(int employeeID, string firstName, string lastName,
string titleOfCourtesy)
this.employeeID = employeeID;
this.firstName = firstName;
this.lastName = lastName;
this.titleOfCourtesy = titleOfCourtesy;
public EmployeeDetails()
当我构建类库并将引用添加到我的 windows 窗体项目时。
这是我的班级出现的主要表格的屏幕截图;但是,没有方法。
【问题讨论】:
【参考方案1】:要绑定到 DataGrid:
-
在表单中添加一个BindingSource (bindingSource)
将 DataGrid 的 DataSource 属性设置为 bindingSource
将 bindingSource 的 DataSource 属性设置为 GetEmployees() 的结果
如果您的库有以下实现,例如:
public interface IDataOperation
List<Employee> GetEmployees();
public class DataOperation : IDataOperation
public List<Employee> GetEmployees()
您的实现应该如下所示:
IDataOperation dataOperation = new DataOperation();
var bindingSource = new BindingSource();
dataGrid.DataSource = bindingSource;
bindingSource.DataSource = dataOperation.GetEmployees();
或者,您可以直接以编程方式将 DataGrid 的 DataSource 属性设置为 GetEmployees() 的结果,而不使用 BindingSource:
dataGrid.DataSource = dataOperation.GetEmployees();
编辑:在您的屏幕截图中,您应该在使用 EmployeeDB 之前对其进行实例化,如下面的代码:
Test.EmployeeDB employeeDB = new Test.EmployeeDB();
dataGrid.DataSource = employeeDB.GetEmployees(); // assuming you have a dataGrid control
【讨论】:
即使我添加了对我的 DLL 的引用并且我的所有类都是 PUBLIC,我有什么理由在我键入 bll 时不会公开我的方法.?????没有显示 是的,有。如果您的 DataOperation 继承自一个接口并且该接口没有声明一个合同 List以上是关于使用 DAL BLL 函数的 Windows 窗体不显示对象的主要内容,如果未能解决你的问题,请参考以下文章