문제

Placing ObsevableCollection User from DataSource as Details on new form it creates all textBoxes, BindingSource and BindingNavigator. Which is excelent and fast.

Because I only need to update one user I removed BindingNavigator. But...

Can this be without a conversion of lists?

class UserDt : Forms {
    // Designer partial part
    this.userBindingSource.DataSource = typeof(WinFormswithEFSample.User);

    private void UserDt_Load
    {
        _context.Users.Load();

        // use this with BindNavigator to navigate ower all users
        //this.userBindingSource.DataSource = _context.Users.Local.ToBindingList();

        // this doesn't work
        //this.userBindingSource.DataSource = _context.Users.Where(p => p.Username == "admin").Local.ToBindingList();

        var query = _context.Users.Where(p => p.Username == "admin").ToList();
        var binding = new BindingList<User>(query);
        this.usersBindingSource.DataSource = binding;
    }
}
도움이 되었습니까?

해결책

Can this be without a conversion of lists?

No.
The BindingList takes an IList as an argument.
IQueryable cannot be casted to an IList, so therefor you need to convert it as you have already done:

    var query = _context.Users.Where(p => p.Username == "admin")
                              .ToList(); //converts the IQueryable to List
    var binding = new BindingList<User>(query);

If you really need the BindingList and cannot settle for a simpler List

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top