Data Mapping DataGridViewComboBoxColumn

In the code below, the "ConnectionType" combo box displays the selected item, but you cannot change the selected item (it seems that there is only one item in the combo box). If I comment the line

typeCol.DataPropertyName = "ConnectionTypeName";

then the combo box can be selected, but the correct item, of course, is not selected. What am I doing wrong?

Thank.

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";
}
0
source share
1 answer

Ultimately, I couldn't get the data binding, entity infrastructure, and combobox to play well, and I just created the brute force of the data grid (code below). This means that I handle insertions, updates, and deletes manually.

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);
    }
}
0
source

All Articles