[英]Sort datagridview by created columns. Entity Framework c# Winforms
I´m having a problem, I retrieve all the Loans I have stored in my database like this: 我遇到了问题,我检索了我存储在数据库中的所有贷款,如下所示:
list_loans = db.Loan.Where(x => x.State.id_state != 6).ToList();
db is the Object context. db是Object上下文。
Then, I assign that list as the DataSource for my DataGridView. 然后,我将该列表指定为DataGridView的DataSource。
dgv_Loans.Datasource = list_loans;
With that info, I add some columns. 有了这些信息,我添加了一些列。 Like for example, installments left to pay. 例如,分期付款。 I get that value by counting the result of a query. 我通过计算查询的结果来获得该值。
The user can order the result using some options. 用户可以使用某些选项订购结果。 Is easy to order the result from the fields that the entity have (using linq), but I dont know how to order the results using this new columns. 很容易从实体拥有的字段(使用linq)订购结果,但我不知道如何使用这个新列来排序结果。
I read some posts here and tried this: 我在这里阅读了一些帖子并尝试了这个:
dgv_Loans.Sort(dgv_Loans.Columns["installments_left"], ListSortDirection.Ascending);
By doing this, I´m getting the following exception at runtime: 通过这样做,我在运行时得到以下异常:
"DataGridView control must be bound to an IBindingList object to be sorted." “DataGridView控件必须绑定到要排序的IBindingList对象。”
Is there anyway to use linq to orderby created columns in a DataGridViewColumn? 无论如何使用linq来命令在DataGridViewColumn中创建列? Or how can I solve this error? 或者我该如何解决这个错误?
I know there are related posts, but after reading them, I can´t find a solution to this specific problem. 我知道有相关的帖子,但在阅读之后,我找不到解决这个具体问题的方法。 Thats why I showed how I implemented to get some advice.. 这就是为什么我展示我如何实施以获得一些建议..
Rather than binding directly to the list retrieved from database, what I generally do is have a view class and have all the calculated properties in that class 而不是直接绑定到从数据库检索的列表,我通常做的是有一个视图类,并具有该类中的所有计算属性
public class LoanView : Loan {
public LoanView(Loan loan){
}
public int InsallmentsLeft { get { return ...; } }
}
and then bind the datasource to a list of this, this keeps sorting working. 然后将数据源绑定到此列表,这将使排序工作。
Concerning about Sort datagridview by created columns using Entity Framework 关于使用Entity Framework 创建的列对datagridview进行排序
I guess you need this Presenting the SortableBindingList<T>
我想你需要这个呈现SortableBindingList<T>
Usage: 用法:
loanBindingSource.DataSource = new SortableBindingList<Loan>(list_loans.ToList());
dgv_Loans.Datasource = loanBindingSource;
int ID = Convert.ToInt32(cmbDepartments.SelectedValue);
var EmployeeList = from Employee in db.Employee
where Employee.DepartmentID == ID
select new
{
Employee.FirstName,
Employee.LastName
};
dataGridView1.DataSource = EmployeeList.ToList();
You could directly give the data source to dataGridView1.DataSource
but you must write ToList()
at the end of your query: 您可以直接将数据源提供给dataGridView1.DataSource
但您必须在查询结束时编写ToList()
:
int ID = Convert.ToInt32(cmbDepartmanlar.SelectedValue);
dataGridView1.DataSource = (from Employee in db.Employee
where Employee.DepartmentID == ID
select new
{
Employee.FirstName,
Employee.LastName
}).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.