开发者

Databinding of DataGridViewComboBoxColumn

开发者 https://www.devze.com 2023-01-12 14:54 出处:网络
In the code below, the combo box named \"ConnectionType\" shows the selected item, but one cannot change the selected item (it seems like there is only one item in the combo box).If I comment out the

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

typeCol.DataPropertyName = "ConnectionTypeName";

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

Thanks.

private void LoadConnectionsGrid()
{
    _dc = new EnterpriseEntit开发者_StackOverflow中文版ies();
    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";
}


Ultimately, I could not get data binding, the entity framework, and comboboxes to play nice and I just created the data grid brute force (code below). This means that I handle inserts, updates and deletes by hand.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号