DataGridViewComboBoxColumn 的数据绑定 [英] Databinding of DataGridViewComboBoxColumn

查看:27
本文介绍了DataGridViewComboBoxColumn 的数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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??

谢谢.

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";
}

推荐答案

最终,我无法让数据绑定、实体框架和组合框发挥良好的作用,我只是创建了数据网格蛮力(下面的代码).这意味着我手动处理插入、更新和删除.

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

这篇关于DataGridViewComboBoxColumn 的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆