下面的类是datagridview组合的数据源:
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
}
下一个是datagridview的数据源:
public class Order
{
public int Id { get; set; }
public int IdCustomer { get; set; }
public DateTime RequestDate { get; set; }
}
为简单起见,我将使用固定值来携带它们,但与我的实际情况类似:
private void Form1_Load(object sender, EventArgs e)
{
bdsCustomer.DataSource = new List<Customer>
{
new Customer {Id = 1, Name = "Paulo", City = "City A"},
new Customer {Id = 2, Name = "Tatiane", City = "City B"},
new Customer {Id = 3, Name = "Paulo", City = "City C"},
};
bdsOrder.DataSource = new List<Order>
{
new Order {Id = 1, IdCustomer = 1, RequestDate = new DateTime(2015, 8, 25)},
new Order {Id = 2, IdCustomer = 2, RequestDate = new DateTime(2015, 8, 26)},
new Order {Id = 3, IdCustomer = 3, RequestDate = new DateTime(2015, 8, 27)}
};
}
我会让这个领域&#34; IdCustomer&#34;在datagridview中显示问题,我将使用组合创建一个新字段:
在图片中注意&#34; IdCustomer&#34;第一条记录的字段是正确的,并显示Id 1:
现在我将选择另一个&#34; Paul&#34;使用Id 3,看看IdCustomer的值会发生什么:
即使选择带有Id 3的客户端,ID也会继续1.您可以拥有10个客户端&#34; Paul&#34;,选择IdCustomer的任何人都不会更改。
我该怎么办?