我想知道如何解决/简化一个任务,有时弹出与DevExpress XtraGrid套件的开发过程中。 有时我陷入这种情况下: 鉴于这些2类,代表了来自业务逻辑层的模型类:XtraGrid绑定复杂和嵌套的对象模型
public class Customer
{
public Int32 CustomerId { get; set; }
public String Name { get; set; }
public String Address { get; set; }
public List<Order> Orders { get; set; }
}
public class Order
{
public Int32 OrderId { get; set; }
public String ItemCode { get; set; }
public Int32 Quantity { get; set; }
public Decimal Price { get; set; }
public DateTime Date { get; set; }
}
我想创建一个简单的窗口,其中显示了XtraGrid中,允许编辑/添加/删除客户列表及其嵌套订单。 为了做到这一点,我用一个GridControl和一个带有AllowAddRow和AllowDeleteRow属性的GridView控件创建了一个简单的表单== true。 然后,在Form1类我已经做了以下内容:
//List of my customers
private List<Customer> _customers;
public Form1()
{
//Initialize UI components
InitializeComponent();
//Call the provider in order to get customers
CustomerProvider cp = new CustomerProvider();
_customers = cp.GetCustomers();
//Initialize bindingSource
BindingSource bs = new BindingSource();
bs.DataSource = _customers;
//Set GridControl's dataSource
gridControl1.DataSource = bs;
}
现在我已经得到了美丽的GridControl显示我的列表的内容。
但现在这里的问题...我怎样才能添加或删除行? 逸岸:
如果我将焦点设置在一个行,我按“Delete”键,它不 工作。
如果我尝试添加一个新行,当它失去焦点时,它突然消失。
显然我失去了一些东西。不幸的是,我发现DevExpress文档对此类论点和最佳实践颇为困惑(在我看来),所以我无法达到我的目标。
有人可以帮助我吗?
PS。 This是我样本的.csproj超链接。
非常感谢您的建议!我试过使用bindingList而不是简单的列表类,它的工作原理...现在我可以添加主行(我的最新示例中的客户行),但它没有解决详细信息(我最新示例中的订单行)。显然,详细信息gridview直接绑定到列表而不是绑定列表的“订单”属性。 –
GiveEmTheBoot
2013-05-03 18:35:43
我如何解决没有改变“订单”属性从列表到BindingList的类型?我不想在我的模型层中使用面向绑定的类型,也不想重新编译我所有的业务逻辑模型容器程序集,因为它们太多了......是否有任何解决方法或方法来包装我的模型使其完全可编辑并将其用作gridcontrol的数据源?谢谢! – GiveEmTheBoot 2013-05-03 18:38:45
没有我知道的。在最高级别上,您的选项是1)使用实现IBindingList接口的容器,2)将控件绑定到数据库,或3)通过DataSet类绑定到XML数据。有关更多信息,请参阅http://documentation.devexpress.com/#WindowsForms/CustomDocument2395。 – kenrogers 2013-05-03 18:54:23