如何使用EF和Repository层对DataGridView进行排序? [英] How to sort in DataGridView using EF and Repository layers?

查看:101
本文介绍了如何使用EF和Repository层对DataGridView进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能做错了事,但让我解释一下我在做什么和我想要什么。我对这一切都很陌生。



我使用EF创建了一个DAL,并使用带有IUnitOfWork接口的Repository Pattern来公开ICustomerProjectServiceViewRepository(这是一个SQL)服务器视图)并使用IUnitOfWork调用方法来提交对存储库的更改。



我现在对提交更改并不感兴趣,只是解释一些事情。 />


我将数据加载到DataGridView中,如下所示:



IEnumerable< CustomerProjectService> CustomerServicesInProject;

UnitOfWork工作区;



workspace = new UnitOfWork(new VeteransNewsNetwork_CoreEntities());
$
CustomerServicesInProject = workspace.CustomerProjectServicesView.GetAll( )。其中(x => x.JobCodeID == JobCodeID).ToList();

dgvCustomerAds.DataSource = CustomerServicesInProject;



我想允许用户单击ColumnHeads来对列进行排序。目前点击时他们什么都不做,我找不到一个属性来让他们排序。

我有,但试过这个:

private void dgvCustomerAds_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)

{

DataGridViewColumn col = dgvCustomerAds.Columns [e.ColumnIndex];
$


//抛出异常:DataGridView控件必须绑定到要排序的IBindingList对象。

dgvCustomerAds.Sort(col,ListSortDirection.Ascending);

}



我是否必须在我想要排序的存储库的DAL中实现IBindingList?或者我是否必须创建自定义集合,如IEnumerable或List<>实现IBindingList?或者我应该知道一种完全不同的方式? I
对此非常陌生,所以对于初学者来说,拥有最简单的解决方案让用户点击标题并能够对当前架构中的DataGridView中的内容进行排序以便我可以看到行动。



我看了一些其他线程,无法真正找到排序的直接解决方案。感谢您的建议!

解决方案

您好HTHP,


据我所知,  ;要为自定义对象实现可排序,必须实现

IComparable

IComparer
界面,用于通知系统您的排序规则是什么,然后排序自定义对象的列表。


此外,  为什么不在数据库级别使用DataGridView? 以下是有关类似问题的链接,供您参考:


Datagridview
从数据库中排序和获取数据


最好的问候,


Bob


I may be doing things incorrectly, but let me explain what I am doing and what I want. I'm really new to all this.

I created a DAL using EF and am using a Repository Pattern with an IUnitOfWork interface to expose the ICustomerProjectServiceViewRepository (which is a SQL Server View) and using IUnitOfWork to call a method to commit changes to repositories.

I'm not really interested right now in committing changes, just explaining things.

I load the data into a DataGridView like this:

IEnumerable<CustomerProjectService>CustomerServicesInProject;
UnitOfWork workspace;

workspace = new UnitOfWork(new VeteransNewsNetwork_CoreEntities());
CustomerServicesInProject = workspace.CustomerProjectServicesView.GetAll().Where(x => x.JobCodeID == JobCodeID).ToList();
dgvCustomerAds.DataSource = CustomerServicesInProject;

I would like to allow the user to click the ColumnHeads to sort the columns. Currently when clicking they do nothing, and I can not find a property to enable them to sort.
I have, however tried this:
private void dgvCustomerAds_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
DataGridViewColumn col = dgvCustomerAds.Columns[e.ColumnIndex];

//Throwing exception: DataGridView control must be bound to an IBindingList object to be sorted.
dgvCustomerAds.Sort(col, ListSortDirection.Ascending);
}

Do I have to implement IBindingList in the DAL on the Repositories that I want to be sortable? Or do I have to create a custom collections like IEnumerable or List<> that implements IBindingList? Or is there a completely different way I should know? I am pretty new to this, so for starters it would be nice to have the easiest solution to enable users to click the headers and be able to sort what is in the DataGridView in the current architecture so I can see it in action.

I've looked at some other threads and can't really find a straight forward solution to sorting. Thanks for the advice!

解决方案

Hi HTHP,

As far as I know, to implement sortable for a custom object you must implement IComparable or IComparer interface to notify to system what is your sort rule , and then to sort the List of the custom object.

In addition, why not use DataGridView at database level ?  Following link about similar issue is for your reference:

Datagridview sorting and fetch data from db

Best Regards,

Bob


这篇关于如何使用EF和Repository层对DataGridView进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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