我想知道如何解决/简化在 DevExpress 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 和 GridView 控件的简单表单,该控件具有 AllowAddRow 和 AllowDeleteRow 属性 == 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 来显示我的列表的内容。
但现在问题来了……我怎样才能添加或删除行?实际上:
如果我将焦点设置在一行上并按“删除”键,则它不起作用。
如果我尝试添加一个新行,当它失去焦点时,它会突然消失。
显然我错过了一些东西。不幸的是,我发现 DevExpress 文档对于这种论点和最佳实践非常混乱(在我看来),所以我无法达到我的目标。
有人可以帮助我吗?
PS。 这是我的示例的 .csproj 的超链接。