数据表 USER 中存放部门信息为部门的编号,在通过数据绑定时显示出来的数据也是编号,有什么方法可以显示为部门名称而不是编号?
员工信息和部门列表 分别放在两个List<User>和List<Department>中,
public class User
{
public int ID { get; set; }
public string UserID { get; set; }
public string Name { get; set; }
public int Sex { get; set; }
public DateTime Birth { get; set; }
public string IDNumber { get; set; }
public string Address { get; set; }
public string Nativeplace { get; set; }
public string Nation { get; set; }
public string Domicile { get; set; }
public string Telephone { get; set; }
public string Email { get; set; }
public int Marriage { get; set; }
public string Education { get; set; }
public string School { get; set; }
public DateTime GraduationDate { get; set; }
public string Specialty { get; set; }
public DateTime EntryDate { get; set; }
public Department Department { get; set; }
public string Duties { get; set; }
public DateTime Departure { get; set; }
public string Password { get; set; }
public int onjob { get; set; }
public int UserType { get; set; } } public class Department
{
public int ID { get; set; }
public string Name { get; set; }
public int SuperiorDeptID { get; set; }
}
List<User> userList = new List<User>();
userList = new UserBLL().UserAll();
gridControl1.DataSource = userList;
员工信息和部门列表 分别放在两个List<User>和List<Department>中,
public class User
{
public int ID { get; set; }
public string UserID { get; set; }
public string Name { get; set; }
public int Sex { get; set; }
public DateTime Birth { get; set; }
public string IDNumber { get; set; }
public string Address { get; set; }
public string Nativeplace { get; set; }
public string Nation { get; set; }
public string Domicile { get; set; }
public string Telephone { get; set; }
public string Email { get; set; }
public int Marriage { get; set; }
public string Education { get; set; }
public string School { get; set; }
public DateTime GraduationDate { get; set; }
public string Specialty { get; set; }
public DateTime EntryDate { get; set; }
public Department Department { get; set; }
public string Duties { get; set; }
public DateTime Departure { get; set; }
public string Password { get; set; }
public int onjob { get; set; }
public int UserType { get; set; } } public class Department
{
public int ID { get; set; }
public string Name { get; set; }
public int SuperiorDeptID { get; set; }
}
List<User> userList = new List<User>();
userList = new UserBLL().UserAll();
gridControl1.DataSource = userList;
private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
{
List<Department> departmentList = new List<Department>();
departmentList = new DepartmentBLL().DepartmentAll();
if (e.Column.FieldName == "Department.ID")
{
foreach (Department dept in departmentList)
{
if (dept.ID.ToString() == e.Value.ToString())
e.DisplayText = dept.Name;
}
}
}
我感觉没有必要这么麻烦LZ,看看我的思路:
首先你可以把显示部门名称的那一列的列属性(ColumnEdit)改成LookUpEdit,Name 属性为Lue_Department,下拉式的。
然后绑定数据源,比如:
List<Department> departmentList = new List<Department>(); departmentList = new DepartmentBLL().DepartmentAll();
this.Lue_Department.DateSource = departmentList
this.Lue_Department.DisplayMember = "Name"; // 部门名称
this.Lue_Department.ValueMember = "ID"; // 部门编号
最后是设置这一列的属性:
在ColumnEdit 属性下找Columns,添加两列,设置其FileName为ID,Name 可是设置ID列的Visible为False。