2011-01-25 78 views
2

我使用BindingNavigator通過datagridview從產品列表中刪除項目。 (方法調用main.DeleteProduct()調用一個存儲庫以從數據庫中刪除)。使用BindingNavigator刪除列表項,不正確的項目

我需要一些幫助來改善..DeleteItem_Click事件的代碼。當我單擊一個單元格/或行,然後刪除按鈕(BindingNavigator)時,它從不刪除該行。它將刪除下面的行,或者如果它是最後一行,則刪除上面的行,如果只有一行,則刪除null。 bindingSource.Current應該不是與datagridview的currentrow相同的項目嗎?

此外,是我使用bindsource鑄造當前項目的一種好方法嗎?如果有的話,會提供更好的代碼建議。

乾杯!

public partial class Form1 : Form 
{  
    private MainBL main = new MainBL(); 
    private List<Product> products = new List<Product> 

    private void Form1_Load(object sender, EventArgs e) 
    { 

     bsProducts.DataSource = products;   // BindingSource 
     bnProducts.BindingSource = bsProducts; // BindingNavigator 
     dataGridView1.DataSource = bsProducts; // 
    } 

    private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e) 
    { 

     Product product = (Product)bsProducts.Current; 

     // Putting a breakpoint here, shows the identity property is not the same 
     // as row selected in datagridview. 

     main.DeleteProduct(product); 

    } 

回答

1

我現在明白該行在CellClick事件觸發前被刪除。因此,我通過將代碼放入刪除按鈕的_MouseDown事件來使其按預期工作。不知道這是雖然最妥善的解決辦法..

private void btnDeleteProducts_MouseDown(object sender, MouseEventArgs e) 
    { 
     Product product = (Product)bsProducts.Current; 
     if (product != null) 
     { 
      main.DeleteProduct(product); 
     }     
    } 
2

更好的解決方案可能是攔截/刪除綁定導航器的刪除事件,並手動處理刪除。

轉到裝訂導航器;調出屬性窗口中的屬性。找到DeleteItem屬性(位於「Items」類別下),並將其設置爲「(none)」。

現在,您可以在相關工具欄中的刪除按鈕的單擊事件中編寫刪除功能。上一個答案中的代碼將起作用 - 您現在可以獲取正確的「當前」項目。如果需要,您可以在此添加確認檢查(「您確定嗎?」)。

當然,不要忘記從BindingSource綁定到的集合中刪除項目(或者只是刷新數據)。

+0

這個答案幫我調試同樣的問題,我與DataGridView中和DeleteItem功能... – ChenChi 2016-08-29 13:02:22

0

我遇到了這個,並做了類似於OP [bretddog]做的事情,但我寫了一個更完整的方法。

我在這裏分享我的工作:

public class YourDataItem 
{ 
    // put all of your data here. This is just stubbed here as an example 
    public int Id { get; set; } 
    public String Description { get; set; } 
} 

private void DeleteBtn_Down(Object sender, MouseEventArgs e) 
{ 
    var item = (YourDataItem)bindingSource1.Current; 
    var dr = DialogResult.None; 
    if (0 < item.Id) 
    { 
     var ask = String.Format("Delete Item [{0}]?", item.Description); 
     dr = MessageBox.Show(ask, "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); 
    } 
    if (dr == DialogResult.Yes) 
    { 
     try 
     { 
      bindingSource1.EndEdit(); 
      _datamodel.YourDataItems.DeleteOnSubmit(item); 
      _datamodel.SubmitChanges(); 
      _datamodel.ClearCache(); 
      bindingSource1.SetPosition<YourDataItem>(x => x.Id == 0); 
     } catch (Exception err) 
     { 
      MessageBox.Show("Database changes failed to complete.", String.Format("Delete {0}", err.GetType()), MessageBoxButtons.OK, MessageBoxIcon.Information); 
     } 
    } else 
    { 
     bindingSource1.CancelEdit(); 
    } 
} 

LINQ的數據源可能會超時。

如果有人在當天回家時碰到刪除按鈕,則在第二天進入並在確認對話框中單擊「確定」,try...catch將處理對象處置異常。

ClearCache()僅僅是一個知名的DataContext擴展:

/// <summary> 
/// Clears the cache from a DataContext to insure data is refreshed 
/// </summary> 
/// <param name="dc"></param> 
public static void ClearCache(this DataContext dc) 
{ 
    dc.GetType().InvokeMember("ClearCache", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, dc, null); 
}