我使用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);
}
這個答案幫我調試同樣的問題,我與DataGridView中和DeleteItem功能... – ChenChi 2016-08-29 13:02:22