C# PropertyGrid C中的列表#
我有一个雇员班。数据库中维护了许多部门,员工可能只属于某个特定部门C# PropertyGrid C中的列表#,c#,.net,winforms,C#,.net,Winforms,我有一个雇员班。数据库中维护了许多部门,员工可能只属于某个特定部门 public class Employee { private string name; private int depID; public string Name { get { return name; } set { name = value; } } public int DepartmentID { get {
public class Employee
{
private string name;
private int depID;
public string Name
{
get { return name; }
set { name = value; }
}
public int DepartmentID
{
get { return depID; }
set { depID = value; }
}
}
public class Department
{
private int depID;
private string depName;
public int DepartmentID
{
get { return depID; }
}
public int DepartmentName
{
get { return depName; }
set { depName = value; }
}
}
如何在PropertyGrid中显示对象Employee,并将部门作为将显示为combobox的属性之一
可能吗?还是有更好的实施方案?
提前感谢您的投入。我为您起草了一个实验(我自己也做了,因为我从来没有这样做过)。对于这个特定的解决方案,它使用Linq来填充手头的组合框,但我相信您也可以用其他方式填充它 我的文档来自“添加域列表和简单下拉属性支持”小节
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
public class Employee : StringConverter
{
DataClasses1DataContext mydb = new DataClasses1DataContext();
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
var a = (from u in mydb.Customers select u.CustomerID).ToArray();
return new StandardValuesCollection(a);
}
public string Name { get; set; }
[TypeConverter(typeof(Employee)), CategoryAttribute("Document Settings")]
public string DepartmentID { get; set; }
}
在我选择的表单加载上:
private void Form1_Load(object sender, EventArgs e)
{
Employee temp = new Employee();
propertyGrid1.SelectedObject = temp;
}
我希望这就是你要找的。值得注意的是,如果愿意,您可以将StringConverter更改为TypeConverter,但我使用了String,因为我处理的字段是字符串
您可以通过实现类型转换器来实现这一点当您说任何更好的实现时,您的意思是您可以放弃PropertyGrid?PropertyGrid中的组合框并不像它看起来那么简单,因此任何其他解决方案都不会比它更糟糕。Alt-PrntScrn只会将活动窗口复制到剪贴板,而不会复制整个桌面。@Lars Pro提示:)我个人认为让视图实现慢慢进入域模型是一种反模式,对我来说,
Employee
似乎是其中的一部分。如果Employee
是视图模型或DTO,这可能是正确的,但我会使用不同的命名约定。这对我来说太过分了:)。我还不熟悉林克。有没有一种非linq方法可以实现这一点?是的,这正是我试图从您的截图中实现的:)如果您不想使用linq,您不必使用linq,您可以通过C#使用SQL,甚至创建一个表适配器并从那里读取。您在上面的示例中看到的唯一要求是变量a是字符串数组。该阵列的源由您决定。