【发布时间】:2013-11-07 02:57:01
【问题描述】:
我已经检查了其他帖子,但仍然无法修复我的问题: 我有两种形式:
- 创建客户(可以有多个电话、地址和汽车)
- 应该显示每个客户信息的主窗体。
首先,是否有可能在每一行中都有一个客户,以及每个客户信息的三个组合(电话、地址和汽车)?
例如:
我有两个客户,John 和 Jack,我希望这些行显示如下:
0 - John - John.Phones - John.Addresses - John.Cars
1 - Jack - Jack.Phones - Jack.Addresses - Jack.Cars
另外,我不知道我应该将什么作为组合框的参数传递给我的行,我应该传递数据源、项目还是组合本身?无论我为组合框传递什么,我都会得到:
System.ArgumentException: DatagridViewRowComboBoxCell value is not valid
这是实际代码。
private DataGridViewRow BuildRow(Cliente cliente)
{
DataGridViewRow row = new DataGridViewRow();
DataGridViewComboBoxCell Autos = new DataGridViewComboBoxCell();
Autos.DataSource = cliente.Autos;
Autos.ValueMember = cliente.Autos[0].ToString();
row.CreateCells(DGCliente, cliente.Codigo.ToString(), cliente.Nombre, Autos);
row.Tag = cliente;
DGCliente.Rows.Add(row);
return row;
}
客户端类代码:(西班牙语)
public class Cliente
{
public String Nombre { get; set; }
public Int32 Codigo { get; set; }
public List<Auto> Autos { get; set; }
public List<Direccion> Direcciones { get; set; }
public List<Telefono> Telefonos { get; set; }
public Cliente()
{
this.Direcciones = new List<Direccion>();
this.Telefonos = new List<Telefono>();
this.Autos = new List<Auto>();
}
public Cliente(String Nombre , Int32 Codigo)
{
this.Nombre = Nombre;
this.Codigo = Codigo;
this.Direcciones = new List<Direccion>();
this.Telefonos = new List<Telefono>();
this.Autos = new List<Auto>();
}
public void AgregarTelefono(bool esCelular, String numero, String area)
{
this.Telefonos.Add(new Telefono(esCelular, numero, area));
}
public void AgregarDireccion(string calle, string altura, string localidad, string provincia)
{
this.Direcciones.Add(new Direccion(calle, altura, localidad, provincia));
}
public void AgregarAuto(Auto auto)
{
this.Autos.Add(auto);
}
}
【问题讨论】:
-
如果方法已经添加了行,为什么要返回该行(如果需要,反之亦然)?此外,缺少变量的命名。
Autos是什么?您将其用作DataGridViewComboBoxCell(简称 DGVCBC)以及您的 Cliente 对象的属性?您可以发布客户代码吗? -
Autos 只是我想给我想添加到行中的组合的名称,我猜应该命名为 DGVCBCAutos
-
你应该有,确实:)。不管怎样,现在你给你的客户 ctor 打了两次电话,并添加了一些电话、地址和汽车,对吧?然后当你调用 buildrow 时,我假设它会崩溃。您可以发布初始化代码,还是来自数据库? (所以我可以在这里测试它)。 (为什么是 altura 而不是 numero 呢??)
标签: c# winforms datagridview datagridviewcombobox