Niall
Niall

Reputation: 1621

Can't change DataGridViewComboBox value

I have a DataGridView which I am populating from a LINQ query. One of the columns is a FK so I have added a ComboBox column to display this field, which is displaying fine.

The problem I am having is that it will not let me change the value of the ComboBox as it acts as if it is locked. I have checked the ReadOnly property of both the DGV and the Column and both are false.

Can anyone shed any light on what I am missing?

The code to populate the DGV is as follows:

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

Many thanks

Upvotes: 1

Views: 617

Answers (1)

Albin
Albin

Reputation: 86

I believe it is because you are using anonymous types. In your case, you could use the original type (type of evt and 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

Or

Create a POCO object to serve as a view model

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}

This will make it easier to implement INotifyPropertyChanging and INotifyPropertyChanged if two-way binding will be necessary.

Upvotes: 5

Related Questions