如何当绑定到链接到EF4实体绑定源进行排序的DataGridView [英] How to sort DataGridView when bound to a binding source that is linked to an EF4 Entity

查看:757
本文介绍了如何当绑定到链接到EF4实体绑定源进行排序的DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接到一个的BindingSource

我的的BindingSource 链接到实体的的IQueryable 清单:

My BindingSource is linked to an IQueryable list of entities:

    public void BindTo(IQueryable elements)
    {
        BindingSource source = new BindingSource();
        source.DataSource = elements;

        bindingNavigator1.BindingSource = source;
        dataGridView1.DataSource = source;

    }



我想我的用户能够点击网格标题对数据 - 努力得到这个工作。是否可以?如果是这样,我怎么办呢?

I am wanting my users to be able to click on the grid headers to sort the data - struggling to get this to work. Is it possible? If so, how do I do it?

推荐答案

我最近与此相同的问题挣扎;似乎IQueryable的接口并没有提供足够的信息为DataViewGrid知道如何对数据进行自动排序,所以你必须无论是从实体源使用的东西它可以使用或者做什么,我没有和处理手动排序功能重新包装您的收藏:

I recently struggled with this same issue; it seems that the IQueryable interface doesn't provide enough information for the DataViewGrid to know how to sort the data automatically; so you have to either repackage your collection from the Entity source using something it can use or do what I did and handle the sorting functionality manually:

      private void myDataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  {
     DataGridViewColumn column = myDataGridView.Columns[e.ColumnIndex];

     _isSortAscending = (_sortColumn == null || _isSortAscending == false);

     string direction = _isSortAscending ? "ASC" : "DESC";

     myBindingSource.DataSource = _context.MyEntities.OrderBy(
        string.Format("it.{0} {1}", column.DataPropertyName, direction)).ToList();

     if (_sortColumn != null) _sortColumn.HeaderCell.SortGlyphDirection = SortOrder.None;
     column.HeaderCell.SortGlyphDirection = _isSortAscending ? SortOrder.Ascending : SortOrder.Descending;
     _sortColumn = column;
  }



我希望帮助。

I hope that helps.

这篇关于如何当绑定到链接到EF4实体绑定源进行排序的DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆