Linq to sql, filtering results in a datagridview

后端 未结 1 1843
野的像风
野的像风 2021-01-20 12:31

I have a very simple database for which I\'m using linq to sql. I have a datagridview to show the contents of the table. I want the user to be able to filter the rows appear

相关标签:
1条回答
  • 2021-01-20 13:23

    To work with DataGridView, you need to implement the non-generic IList, not the generic IList<T> (or simpler and better: inherit from BindingList<T>, which provides things like change notifications via INotifyPropertyChanged). For working with LINQ-to-SQL I have some info on usenet that might be useful (assuming it still holds water - it has been a while).

    re "rest of the problem"... can you be more specific?

    Re filtering LINQ-to-SQL efficiently, you don't want to use Predicate<T>; you want to use Expression<Func<T,bool>>; this allows you to pass this down to the database via Queryable.Where, i.e. (where you have an IQueryable<T> source) something like:

    IQueryable<T> data = tableSource;
    // then for each filter "expr"
    {
      data = data.Where(expr);
    }
    

    Writing a true filtered list is very tricky. I've done it for in-memory objects (I can't post the code, though) - but it takes a lot of object tracking etc. Unless you absolutely need this, it may be easier to keep things simple and just display simple snapsnots, tracking just additions/removals. For simple snapshots, just ToBindingList() may suffice...

    0 讨论(0)
提交回复
热议问题