Using C #, with EntityFramework and codefirst, I have the following classes in my models layer:
public class Cliente
{
public int ClienteId { get; set; }
public string Nome { get; set; }
public string Email { get; set; }
public DateTime DataCadastro { get; set; }
public bool Ativo { get; set; }
public virtual ICollection<Endereco> Enderecos { get; set; }
public virtual ICollection<Telefone> Telefones { get; set; }
}
public class Endereco
{
public int EnderecoId { get; set; }
public string Cep { get; set; }
public string Rua { get; set; }
public string Numero { get; set; }
public string Complemento { get; set; }
public string Bairro { get; set; }
public string Cidade { get; set; }
public string Estado { get; set; }
public decimal TaxaDeEntrega { get; set; }
public int ClienteId { get; set; }
public virtual Cliente Cliente { get; set; }
}
public class Telefone
{
public int TelefoneId { get; set; }
public string Numero { get; set; }
public int ClienteID { get; set; }
public virtual Cliente Cliente { get; set; }
}
In my Forms layer, I have a form with a datagridview with the columns of the client name, email, date of registration and a combobox with the lists of telephones of each one. These settings are set up as follows:
private void FormClientes_Load(object sender, EventArgs e)
{
//Não exibe os ícones de Maximixação e minimização do form dentro de uma MDI Parent
this.FormBorderStyle = FormBorderStyle.None;
this.ControlBox = false;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.ShowIcon = false;
this.Dock = DockStyle.Fill;
dataGridViewClientes.AutoGenerateColumns = false;
dataGridViewClientes.RowHeadersVisible = false;//Omite a primeira coluna
DataGridViewTextBoxColumn colNome = new DataGridViewTextBoxColumn();
colNome.HeaderText = "NOME";
colNome.DataPropertyName = "Nome";
dataGridViewClientes.Columns.Add(colNome);
DataGridViewTextBoxColumn colEmail = new DataGridViewTextBoxColumn();
colEmail.HeaderText = "EMAIL";
colEmail.DataPropertyName = "Email";
dataGridViewClientes.Columns.Add(colEmail);
DataGridViewTextBoxColumn colDataCadastro = new DataGridViewTextBoxColumn();
colDataCadastro.HeaderText = "DATA CADASTRO";
colDataCadastro.DataPropertyName = "DataCadastro";
dataGridViewClientes.Columns.Add(colDataCadastro);
DataGridViewComboBoxColumn colTelefones = new DataGridViewComboBoxColumn();
colTelefones.HeaderText = "TELEFONES";
colTelefones.DataPropertyName = "Numero";
dataGridViewClientes.Columns.Add(colTelefones);
FillDataGrid();
}
private void FillDataGrid()
{
foreach (var c in _clienteApp.GetAll())
{
DataGridViewComboBoxCell cbc = new DataGridViewComboBoxCell();
foreach (var t in c.Telefones)
{
cbc.Items.Add(t.Numero);
}
dataGridViewClientes.Rows.Add(c.Nome, c.Email, c.DataCadastro, cbc);
}
}
In the FillDataGrid()
function I get the DB data and populate the columns. The problem is when I'm going to add the combobox of phones to the end of the line. The program compiles but loading the form the combo is not mounted and I have the following exception:
IfIremovetheDataGridViewComboBoxCell
thelinesaremountedbutthecomboboxisleftwithoutthephones.I'vealsotriedtomountaCombobox
andaddittotheendofthedataGridViewClientes.Rows.Add(c.Nome,c.Email,c.DataCadastro,...);
linebutIdidnotsucceed.WhereamIgoingwrong?