无法更改DataGridViewComboBox值

时间:2012-11-23 10:24:30

标签: c# winforms datagridview

我有一个DataGridView,我从LINQ查询填充。其中一列是FK,所以我添加了一个ComboBox列来显示这个字段,显示正常。

我遇到的问题是它不会让我更改ComboBox的值,因为它的行为就好像被锁定一样。我检查了DGV和Column的ReadOnly属性,两者都是假的。

任何人都可以了解我所缺少的内容吗?

填充DGV的代码如下:

    private void PopulateForm()
    {
        DBDataContext db = new DBDataContext();

        var eventTypes =
            from evt in db.EVENT_TYPEs
                .Where(a => a.Omit == false)
                .OrderBy(a => a.EventType)
            select new
            {
                EventTypeID = evt.EventTypeID,
                EventType = evt.EventType,
                UpdateToStatusID = evt.UpdateToStatusID,
                AttachmentAllowedYn = evt.AttachmentAllowedYn,
                AttachmentRequiredYn = evt.AttachmentRequiredYn,
                CommentRequiredYn = evt.CommentRequiredYn
            };

        var statuses =
            from sts in db.STATUS
                .Where(a => a.Omit == false)
            select new
            {
                StatusID = sts.StatusID,
                Status = sts.TechreqStatus
            };

        DataGridView dgv = this.dgvEventTypes;
        DataGridViewColumn col;

        dgv.AutoGenerateColumns = false;
        col = dgv.Columns[dgv.Columns.Add("EventTypeID", "EventTypeID")];
        col.DataPropertyName = "EventTypeID";

        col = dgv.Columns[dgv.Columns.Add("EventType", "Event Type")];
        col.DataPropertyName = "EventType";

        DataGridViewComboBoxColumn comboCol = new DataGridViewComboBoxColumn();
        comboCol.Name = "UpdateToStatusID";
        comboCol.HeaderText = "Update To Status";
        comboCol.DataPropertyName = "UpdateToStatusID";
        comboCol.DataSource = statuses;
        comboCol.ValueMember = "StatusID";
        comboCol.DisplayMember = "Status";
        dgv.Columns.Add(comboCol);

        col = dgv.Columns[dgv.Columns.Add("AttachmentAllowedYn", "Attachments Allowed Yn")];
        col.DataPropertyName = "AttachmentAllowedYn";

        col = dgv.Columns[dgv.Columns.Add("AttachmentRequiredYn", "Attachment Required Yn")];
        col.DataPropertyName = "AttachmentRequiredYn";

        col = dgv.Columns[dgv.Columns.Add("CommentRequiredYn", "Comment Required Yn")];
        col.DataPropertyName = "CommentRequiredYn";

        dgv.DataSource = eventTypes;

        db.Dispose();
    }

非常感谢

1 个答案:

答案 0 :(得分:5)

我相信这是因为你使用的是匿名类型。 在您的情况下,您可以使用原始类型(evt和sts的类型)

var eventTypes = from evt in db.EVENT_TYPEs where !evt.Omit order by evt.EventType
                 select evt

var statuses = from sts in db.STATUS where !sts.Omit
               select sts

创建一个POCO对象作为视图模型

var eventTypes = from evt in db.EVENT_TYPEs where !evt.Omit order by evt.EventType
                 select new EventTypesObject() {EventTypeID = evt.EventTypeID, ...}

var statuses = from sts in db.STATUS where !sts.Omit
               select new StatusObject() {StatusID = sts.StatusID, Status = sts.TechreqStatus}

如果需要双向绑定,这将使实现INotifyPropertyChanging和INotifyPropertyChanged变得更容易。