DataGridViewComboBoxColumn的数据绑定

时间:2010-08-23 21:16:11

标签: winforms

在下面的代码中,名为“ConnectionType”的组合框显示所选项目,但无法更改所选项目(似乎组合框中只有一个项目)。如果我注释掉这行

typeCol.DataPropertyName = "ConnectionTypeName";

然后组合框 可选,但当然没有选择正确的项目。我做错了什么?

感谢。

private void LoadConnectionsGrid()
{
    _dc = new EnterpriseEntities();
    dataGridViewConnections.AutoGenerateColumns = false;
    dataGridViewConnections.DataSource = _dc.Connection.Include("ConnectionType");

    DataGridViewComboBoxColumn typeCol =
        (DataGridViewComboBoxColumn)dataGridViewConnections.Columns["ConnectionType"];
    typeCol.DataPropertyName = "ConnectionTypeName";
    var qry = from c in _dc.ConnectionType
              select c.Type;
    typeCol.DataSource = qry;

    DataGridViewTextBoxColumn nameCol =
        (DataGridViewTextBoxColumn)dataGridViewConnections.Columns["ConnectionName"];
    nameCol.DataPropertyName = "Name";

    DataGridViewTextBoxColumn connStrCol =
        (DataGridViewTextBoxColumn)dataGridViewConnections.Columns["ConnectionString"];
    connStrCol.DataPropertyName = "ConnectionString";
}

1 个答案:

答案 0 :(得分:0)

最终,我无法获得数据绑定,实体框架和组合框来发挥很好,我只是创建了数据网格暴力(下面的代码)。这意味着我手工处理插入,更新和删除。

private void LoadConnectionsGrid()
{
    DataGridViewComboBoxColumn typeCol =
        (DataGridViewComboBoxColumn)dataGridViewConnections.Columns["ConnectionType"];
    var qry = from c in _dc.ConnectionType
              select c.Type;
    typeCol.DataSource = qry;

    dataGridViewConnections.Rows.Clear();
    foreach (Connection conn in _dc.Connection.Include("ConnectionType"))
    {
        dataGridViewConnections.Rows.Add(conn.Name, 
            conn.ConnectionType.Type, conn.ConnectionString);
    }
}