2009-10-20 73 views
0

後,我有綁定到這勢必DataContext表,像這樣的BindingSource網格:清爽的BindingSource插入(LINQ to SQL中)

myBindingSource.DataSource = myDataContext.MyTable; 
myGrid.DataSource = myBindingSource; 

我不能插入後刷新BindingSource。這不起作用:

myDataContext.Refresh(RefreshMode.OverwriteCurrentValues, myBindingSource); 
myBindingSource.ResetBinding(false); 

無論是這樣的:

myDataContext.Refresh(RefreshMode.OverwriteCurrentValues, myDataContext.MyTable); 
myBindingSource.ResetBinding(false); 

我應該怎麼辦?

回答

2

我已經解決了這個問題,但沒有以我想要的方式。

原來,DataContext和Linq To SQL最適合工作單元操作。意味着你創建一個DataContext,完成你的工作,放棄它。如果您需要其他操作,請創建另一個操作。

對於這個問題,我唯一需要做的就是像this.dx = new MyDataContext();重新創建我的DataContext。如果你不這樣做,你總會得到陳舊/緩存的數據。從我從各種博客/論壇帖子讀到的DataContext是輕量級的,並且正在做這個A-OK。這是我搜索一天後找到的唯一方法。

0

我想你還應該刷新/更新datagrid。你需要強制重繪網格。

1

最後還有一個工作解決方案。

此解決方案正常工作,不需要重新創建DataContext。

  1. 您需要重置內部表緩存。 爲此,您需要使用反射更改表的私有屬性cachedList。

您可以使用以下工具的代碼:使用的東西

public static class LinqDataTableExtension 
    { 
     public static void ResetTableCache(this ITable table) 
     { 
      table.InternalSetNonPublicFieldValue("cachedList", null); 
     } 

     public static void ResetTableCache(this IListSource source) 
     { 
      source.InternalSetNonPublicFieldValue("cachedList", null); 
     } 

     public static void InternalSetNonPublicFieldValue(this object entity, string propertyName, object value) 
     { 
      if (entity == null) 
       throw new ArgumentNullException("entity"); 
      if(string.IsNullOrEmpty(propertyName)) 
       throw new ArgumentNullException("propertyName"); 

      var type = entity.GetType(); 
      var prop = type.GetField(propertyName, BindingFlags.NonPublic | BindingFlags.Instance); 
      if (prop != null) 
       prop.SetValue(entity, value); 
// add any exception code here if property was not found :) 
     } 
    } 

,如:

var dSource = Db.GetTable(...) 
    dSource.ResetTableCache(); 
  • 則需要使用重置您的BindingSource類似於:

    _BindingSource.DataS ource = new List(); _BindingSource.DataSource = dSource; //黑客 - 刷新綁定列表

  • 享受:)

  • 1

    我有同樣的問題。我正在使用表單在我的表中創建行,而不是每次都保存上下文。幸運的是,我有多種形式做這個,一個更新了網格,一個沒有。

    唯一的區別是什麼?
    我一個綁定到實體類似的(未使用的BindingSource)給你做了什麼:

    myGrid.DataSource = myDataContext.MyTable; 
    

    第二我約束:

    myGrid.DataSource = myDataContext.MyTable.ToList(); 
    

    第二種方式工作。

    1

    網格數據源通過新的查詢而不是僅競賽表格進行更新。 簡單解決方案<但工作。

    這是例如。 !!!!!謝謝 - 問題解決後沒有天!但如此簡單的方式..

    CrmDemoContext.CrmDemoDataContext Context = new CrmDemoContext.CrmDemoDataContext(); 
    
    var query = from it in Context.Companies select it; 
    // initial connection 
    dataGridView1.DataSource = query; 
    

    後改變或添加數據

    Context.SubmitChanges(); 
    //call here again 
    dataGridView1.DataSource = query; 
    
    0

    不知道你怎麼插入行。當使用DataContext.InsertOnSubmit(row)時,我遇到了同樣的問題,但是當我將行插入BindingSource而不是BindingSource.Insert(Bindingsource.Count, row) 並僅將DataContext用於DataContext.SubmitChanges()DataContext.GetChangeSet()時。 BindingSource將行插入到網格和上下文中。

    0

    Atomosk答案幫我解決了類似的問題 - 非常感謝Atomosk

    我用下面的代碼兩行更新我的數據庫,但在DataGridView沒有表現出改變(它沒有添加新行):

    this.dataContext.MyTable.InsertOnSubmit(newDataset); 
    this.dataContext.SubmitChanges(); 
    

    this.dataContext.MyTable設定爲一個的BindingSource對象,它被設置爲一個的DataGridView對象的數據源屬性的數據源屬性。 在代碼它看起來像這樣:

    DataGridView dgv = new DataGridView(); 
    BindingSource bs = new BindingSource(); 
    bs.DataSource = this.dataContext.MyTable; // Table<T> object type 
    dgv.DataSource = bs; 
    

    設置bs.DataSource等於,之後回到this.dataContext.MyTable無助於任何更新的DataGridView。

    更新與新條目在DataGridView的唯一途徑是完全不同的方法將其添加到BindingSource,而不是在DataContext的對應表,提到Atomosk

    this.bs.Add(newDataset); 
    this.dataContext.SubmitChanges(); 
    

    沒有這樣做bs.Count;返回數量較少this.dataContext.MyTable.Count(); 這是沒有意義的,似乎是在我看來綁定模型的錯誤。