0
votes

Using C#, with EntityFramework and codefirst, I have follow classes on 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, I have a form with a datagridview with columns with client name, email, date and a combobox with a telephones list for each client that can have more than one telephone. These configurations was mounted like this:

private void FormClientes_Load(object sender, EventArgs e)
        {
            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);
            }
        }

The FillDataGrid() method I take BD data and fill the columns. The problem is when I go to add the telephones combobox in end of row. The program compile, but when load the form, the combobox is not monted and the following exception is showed: System.ArgumentException: The DataGridViewComboboxCell is not valid

inserir a descrição da imagem aqui

If I do not put the DataGridViewComboBoxCell the rows is mount, but the combobox do not show telephones. Also try to create a combobox and add it on end of dataGridViewClientes.Rows.Add(c.Nome, c.Email, c.DataCadastro, ... ); but witout success. Where I make a mistake yet? ? inserir a descrição da imagem aqui

1

1 Answers

1
votes

I believe the issue is from the line…

dataGridViewClientes.Rows.Add(c.Nome, c.Email, c.DataCadastro, cbc);

Adding the combo box cell this way appears to throw this error. A workaround is to set the cell manually. It IS a combo box column, so it should be valid.

dataGridViewClientes.Rows[curRow].Cells["Numero"] = cbc;

To help, a row index curRow is used to keep track of each client, one per row. The code below demonstrates my comments above. I added the line below to the phone columns properties to allow the “Numero” reference

colTelefones.Name = "Numero";

Hope this helps.

private void FillDataGrid() {
  int curRow = 0;
  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);
    dataGridViewClientes.Rows[curRow].Cells["Numero"] = cbc;
    curRow++;
  }
}