2013-06-28 50 views
0

我有一個datagridview,我想綁定一個linq查詢。綁定實體到datagridview,並允許編輯和刪除

這是在我的職業等級:

private readonly SuburbanPortalEntities _entities; 
    private List<PaymentType> _paymentTypes; 

這裏是我的加載代碼:

_paymentTypes = (from pt in _entities.PaymentTypes 
      where pt.CorporationId.Equals(_currentcorp.CorporationId) 
      select pt).ToList(); 
    dataGridView_PaymentTypes.DataSource = _paymentTypes; 

這是我的說明書,其在這一點上,做什麼都沒有:

private void button_Insert_Click(object sender, EventArgs e) 
{ 
    var row = new PaymentType(); 
    row.IsActive = false; 
    row.IsAdded = true; 
    row.CorporationId = _currentcorp.CorporationId; 
    row.TokenId = _token.TokenId; 
    row.PaymentTypeId = Guid.NewGuid(); 
    row.ExcludeCreditCodes = 9; 
    _paymentTypes.Add(row); 
} 

而且我簡單的保存按鈕:

_entities.SaveChanges(); 

它正在處理現有記錄。當我對它們進行更改時,它會更改數據庫中的數據。它不適用於新記錄,我也沒有刪除,因爲我不知道如何去做。

我寧願讓datagridview在行上添加記錄而不是按鈕。另外,通過刪除,允許datagridview處理刪除。

有什麼建議嗎?

回答

0

用於插入新的數據到SQL Server,你可以試試這個:

private void button_Insert_Click(object sender, EventArgs e) 
{ 
    var row = new PaymentType(); 
    row.IsActive = false; 
    row.IsAdded = true; 
    row.CorporationId = _currentcorp.CorporationId; 
    row.TokenId = _token.TokenId; 
    row.PaymentTypeId = Guid.NewGuid(); 
    row.ExcludeCreditCodes = 9; 
    _entities.PaymentTypes.Add(row); //for linq to sql, you should use InsertOnSubmit() 
    _entities.SaveChanges(); 
} 
相關問題