【问题标题】:Delete list item with BindingNavigator, not correct item使用 BindingNavigator 删除列表项,项目不正确
【发布时间】:2011-01-25 16:06:00
【问题描述】:

我使用 BindingNavigator 通过 datagridview 从产品列表中删除项目。 (方法调用 main.DeleteProduct() 调用存储库从数据库中删除)。

我需要一些帮助来改进 ..DeleteItem_Click 事件的代码。当我单击单元格/或行,然后删除按钮(BindingNavigator)时,它永远不会删除该行。它删除下面的行,或者如果它是最后一行,则删除上面的行,如果只有一行,则强制转换为 null。 bindingSource.Current 不应该与 datagridview 的当前行相同吗?

另外,我使用 bindingsource 投射当前项目的方式是一种好方法吗?如果有的话,会欣赏更好的代码建议。

干杯!

 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);

    }

【问题讨论】:

    标签: c# .net binding bindingsource


    【解决方案1】:

    更好的解决方案可能是拦截/删除绑定导航器的删除事件,并手动处理删除。

    转到绑定导航器;在“属性”窗口中调出属性。找到 DeleteItem 属性(在“Items”类别下),并将其设置为“(none)”。

    现在您可以在相关工具栏中的删除按钮的单击事件中编写删除功能。上一个答案中的代码将起作用 - 您现在可以获得正确的“当前”项目。如果需要,您也可以在此处添加确认检查(“您确定吗?”)。

    当然,不要忘记从绑定了 BindingSource 的集合中移除项目(或者只是刷新数据)。

    【讨论】:

    • 这个答案帮助我调试了与 DataGridView 和 DeleteItem 函数相同的问题...
    【解决方案2】:

    我现在明白该行在 CellClick 事件触发之前已被删除。因此,我通过将代码放在删除按钮的 _MouseDown 事件中来使其按预期工作。不确定这是否是最合适的解决方案..

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

    【讨论】:

      【解决方案3】:

      我遇到了这个,做了一些类似于 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 将处理 Object Disposed 异常。

      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);
      }
      

      【讨论】:

        猜你喜欢
        • 2016-09-20
        • 1970-01-01
        • 2021-07-17
        • 1970-01-01
        • 1970-01-01
        • 2011-03-01
        • 2018-09-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多