使用C#,用的EntityFramework和codefirst,我有我的模型層後續類:如何把在datagridview的組合框編程迭代集合
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; }
}
在我的形式,我有一個datagridview的一種形式包含客戶名稱,電子郵件,日期和帶有電話列表的組合框,每個客戶可以有多個電話。這些構型被安裝是這樣的:
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);
}
}
的FillDataGrid()
方法予取數據BD和填充列。問題是當我在行的末尾添加電話組合框時。程序編譯,但是當負載形式,組合框不monted及以下異常顯示信息:System.ArgumentException:的的DataGridViewComboBoxCell無效
如果我不把DataGridViewComboBoxCell
行是安裝,但組合框不顯示電話。也嘗試創建一個組合框,並在dataGridViewClientes.Rows.Add(c.Nome, c.Email, c.DataCadastro, ...);
結束時添加它,但取得成功。我犯了什麼錯誤? ?
謝謝。我遵循他們的建議,一切都按我的預期工作。我也在葡萄牙語中引用了您在Stackoverflow中的答案的鏈接,但是您可以閱讀源代碼:)(https://pt.stackoverflow.com/questions/218925/colocar-uma-combobox-numa-coluna-de-um -datagridview/219066#219066)所以其他同事可以分享你的答案。在那裏你還可以看到最終的代碼是如何的。再一次,謝謝。 – Emerson
@ Emerson ...很高興幫助,我理解你的挫敗感,'DataGridViewComboBoxCell'有時可以忍受。祝你好運! – JohnG