2016-07-05 67 views
0

我有這個使用JOIN的EF數據源的RadGrid,在查詢中使用JOIN時更新實體

var result = db.Programmes.Join(db.Faculties, prog => prog.FacultyID, fac => fac.ID, 
(prog, fac) => new { 
       ID = prog.ID, 
       FacultyID = prog.FacultyID, 
       FacultyText = fac.FacultyName_mk, 
       StudyLevelID = prog.StudyLevelID, 
       Ects = prog.Ects 
      }).ToList(); 
RadGrid1.DataSource = result; 

到目前爲止這麼好。我有正確的數據顯示。

問題來了,當我想插入一個新的項目(程序)或更新現有的例如

protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e) 
{ 
    GridEditableItem item = e.Item as GridEditableItem; 
    // get the ID 
    Int32 id = (Int32)item.GetDataKeyValue("ID"); 
    // get the current record 
    var record = db.Programmes.Where(r => r.ID == id).FirstOrDefault(); 
    //update entity's information with what was entered in the RadGrid 
    item.UpdateValues(record); 
    db.SaveChanges(); 
    // close the edit form 
    RadGrid1.MasterTableView.ClearEditItems(); 
} 

這個東西更新除了FacultyID應該從RadComboBox控件採取的所有列;

<EditItemTemplate> 
    <telerik:RadComboBox runat="server" ID="cmbFacultyID" /> 
</EditItemTemplate> 

我從ItemDataBound中出價組合,它從Faculties實體讀取。

我該如何更新從此ComboBox中讀取FacultyID的程序表?

謝謝你這麼多

回答

0

事實證明,我只是需要更新的記錄之後,我從查詢例如分配新選擇教師獲得

// get the current record 
var record = db.Programmes.Where(r => r.ID == id).FirstOrDefault(); 
// FIX 
record.FacultyID = Convert.ToInt32(facultyid.SelectedValue); 
// update entity's information with what was entered in the RadGrid 
item.UpdateValues(record); 

工作正常現在:) 謝謝