【问题标题】:Sort "Master-Detail" DataGridView bound to Entity Framework in Winforms在 Winforms 中对绑定到实体框架的“主从”DataGridView 进行排序
【发布时间】:2012-03-30 01:33:29
【问题描述】:

我正在用 C# .NET 4.0 编写一个小型企业应用程序。我使用 SQL Server CE 4.0 作为我的数据库。

我使用实体框架与数据库进行双向通信。我的 datagridviews 绑定到实体框架集合,因此用户可以直接在 datagridview 中添加新数据或修改现有数据。问题是不真正支持使用绑定到 datagridview 的实体框架进行排序。根据我所学到的:

  1. 我可以拦截对列标题单元格的点击,然后执行排序并将结果重新绑定到 datagridview。这有点乏味,但它适用于主数据网格视图。但是当我这样做也是为了“细节”DataGridView“,然后我松开”详细信息“DataGridViews的自动rebinding(选择了从主表中的新行时)。所以我还要处理它。

    li >
  2. 我可以将查询转换为列表/绑定列表并将其传递给可排序的绑定列表。那么在这里我手动重新绑定“详细”数据网格视图有同样的问题。这里出现的新问题是,现在我必须以某种方式修复保存,因为新数据仅添加到可排序的绑定列表中,而不是直接添加到实体框架上下文中。

我应该做什么(以及如何做)?我应该只使用数据集吗?

【问题讨论】:

    标签: winforms entity-framework sorting datagridview master-detail


    【解决方案1】:

    我的偏好是使用包含已排序实体的中间 [observable] 集合。然而,这是在 WPF/MVVM 世界中。即使这样,对于使用集合的 ASP.NET ObjectDataSource 或 MVC,Pattern 仍然相对相同。已经有一段时间了,但也许我可以脑补一下,希望你能找到有用的东西。

    我是从记忆中提取出来的,所以记住这只是为了帮助你指明某个方向。

    我们将使用的表单变量。

    private string SortProperty { get; set; }
    private ListSortDirection SortDirection { get; set; }
    private ICollection<myItems> items; // Entity Collection
    private ObservableCollection<myItems> SortedItems { get; set; } // Sorted Collection
    

    重载表单 OnLoad 事件处理程序以注册标题单击处理程序以应用排序。

    protected override void OnLoad(EventArgs e)
    {
        dataGridView1.ColumnHeaderMouseClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(dataGridView1_ColumnHeaderMouseClick);
    
        LoadDataGridView();
        base.OnLoad(e);
    }
    
    protected override void OnUnload(EventArgs e)
    {
        dataGridView1.ColumnHeaderMouseClick -= new System.Windows.Forms.DataGridViewCellMouseEventHandler(dataGridView1_ColumnHeaderMouseClick);
        base.OnUnload(e);
    }
    

    执行我们对排序数据的初始加载。

    private void LoadDataGridView()
    {
        items = myRepository.GetAllItems(); // However you get or have your collection of items.
        ApplySort();
        dataGridView1.DataSource = SortedItems;
    }
    

    对我们的数据进行排序并保存在新集合中。 OrderBy 需要动态查询库:http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

    private void ApplySort()
    {
        // IQueryable<myItems>, ICollection<myItems>, ObservableCollection<myItems>... be aware of cross threading and how you will handle updates.
        SortedItems = items.AsQuerable().OrderBy(SortProperty + (SortDirection == ListSortDirection.Ascending ? " asc" : " desc")).ToList();
    
    }
    

    我们的点击事件处理程序。请记住,您必须处理添加、删除和更改的实体。

    private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        var propertyName = GetPropertyName(dataGridView1.Columns[e.ColumnIndex])
        SortDirection = SortProperty == propertyName ?
                SortDirection == ListSortDirection.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending
            : SortDirection;
        SortProperty = propertyName;
        ApplySort();
        dataGridView1.DataSource = SortedItems;
    }
    

    简单的排序辅助方法。

    private string GetPropertyName(int columnNumber)
    {
        switch(columnNumber)
        {
            case 0:
                return "Id";
            case 1:
                return "Name";
            default:
                return "Id";
        }
    }
    

    以下是一些附加信息: http://msdn.microsoft.com/en-us/library/ms171608.aspx

    我知道这不是一个确切的答案,但由于没有其他人会加入,也许你可以接受这个,修改它直到它正常工作,然后为下一个人评论你的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-05
      • 2011-08-19
      • 2011-08-13
      • 1970-01-01
      • 2011-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多