如何在绑定到链接到EF4实体的绑定源时对DataGridView进行排序

时间:2010-12-16 17:16:22

标签: c# winforms entity-framework sorting datagridview

我有一个与DataGridView相关联的BindingSource

我的BindingSource已链接到IQueryable实体列表:

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

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

    }

我希望我的用户能够点击网格标题来对数据进行排序 - 努力使其发挥作用。可能吗?如果是这样,我该怎么办?

5 个答案:

答案 0 :(得分:12)

我最近在同样的问题上苦苦挣扎;似乎IQueryable接口没有为DataViewGrid提供足够的信息来知道如何自动排序数据;所以你必须使用它可以使用的东西或者我做的事情从Entity源重新打包你的集合并手动处理排序功能:

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

我希望有所帮助。

答案 1 :(得分:1)

<强> VB.NET

如果您使用带有linq语法的bindingsource,则可以像这样对数据进行排序

在这种情况下,从实体框架对象“NCFile”加载与datagridview关联的绑定源时,将外部列添加到“NCFilePartSet”列表中

bsFileSections.DataSource = From ncfps In NCFile.NCFilePartSet Order By ncfps.Sort Select ncfps 

或者像这样

bsFileSections.DataSource = NCFile.NCFilePartSet.OrderBy(Function(ncfps) ncfps.Sort)

其中“Sort”是NCFilePartSet中的一列

实体更新继续工作并反映回数据库

答案 2 :(得分:1)

是的, 可以在绑定到EF数据时轻松拥有可排序的DGV。使用BLW library中的BindingListView(另请查看How do I implement automatic sorting of DataGridView?)。

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

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

}

在我的测试中,即使在构造函数中调用.ToList()(如上所述),更改也会传播到数据库,这让我感到惊讶。

答案 3 :(得分:1)

此代码段效果非常好,并且对于大多数用途来说足够快......

int iColNumber = 3; //e.g., sorting on the 3rd column of the DGV

MyBindingSource.DataSource = MyBindingList.OrderByDescending(o => o.GetType().GetProperty(MyDataGridView.Columns[iColNumber].Name).GetValue(o));

答案 4 :(得分:0)

也许这会对您有所帮助。

internal class CustomDataGridView : DataGridView
{   
    public SortOrder MySortOrder { get; set; }
    protected override void OnColumnHeaderMouseClick(DataGridViewCellMouseEventArgs e)
    {
        BindingSource MyBindingSource = (BindingSource)base.DataSource;
        DataTable MyDataTable = (DataTable)MyBindingSource.DataSource;
        switch (MySortOrder)
        {
            case SortOrder.None:
                MyDataTable.DefaultView.Sort = base.Columns[e.ColumnIndex].Name + " ASC";
                MyDataTable = MyDataTable.DefaultView.ToTable();
                MyBindingSource.DataSource = MyDataTable;
                MySortOrder = SortOrder.Ascending;
                break;

            case SortOrder.Ascending:
                MyDataTable.DefaultView.Sort = base.Columns[e.ColumnIndex].Name + " DESC";
                MyDataTable = MyDataTable.DefaultView.ToTable();
                MyBindingSource.DataSource = MyDataTable;
                MySortOrder = SortOrder.Descending;
                break;

            case SortOrder.Descending:
                MyDataTable.DefaultView.Sort = Properties.Settings.Default.OderDataGridView; //SqlOriginOrder
                MyDataTable = MyDataTable.DefaultView.ToTable();
                MyBindingSource.DataSource = MyDataTable;
                MySortOrder = SortOrder.None;
                break;
        }
        base.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection = MySortOrder; //mini arrow 
    }
}
相关问题