2012-11-23 70 views
1

我有一個DataGridView,我從LINQ查詢填充。其中一列是FK,所以我添加了一個ComboBox列來顯示這個顯示正常的字段。無法更改DataGridViewComboBox值

我遇到的問題是,它不會讓我改變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(); 
    } 

非常感謝

回答

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。

+0

完美,這正是我所錯過的,這已經解決了我的問題。非常感謝! – CrazyHorse