WPF工具包DataGrid到LINQ到SQL查询的数据绑定
通过LINQ to SQL将WPF Toolkit DataGrid绑定到SQL数据库时,如何正确设置绑定源:WPF工具包DataGrid到LINQ到SQL查询的数据绑定,wpf,linq-to-sql,generics,data-binding,datagrid,Wpf,Linq To Sql,Generics,Data Binding,Datagrid,通过LINQ to SQL将WPF Toolkit DataGrid绑定到SQL数据库时,如何正确设置绑定源: 它应该是由LINQ to SQL查询填充和更新的某个通用集合,还是可以直接将DataGrid与LINQ to SQL查询连接起来?与往常一样,剥猫皮的方法不止一种。但是,我更喜欢创建一个LINQ to SQL查询,并使用.ToList()将列表发送到ObservableCollection,这样您就可以监视数据的更改,并在数据变脏时更新SQL Server数据库。换句话说,它看起来是这
它应该是由LINQ to SQL查询填充和更新的某个通用集合,还是可以直接将DataGrid与LINQ to SQL查询连接起来?与往常一样,剥猫皮的方法不止一种。但是,我更喜欢创建一个LINQ to SQL查询,并使用.ToList()将列表发送到ObservableCollection,这样您就可以监视数据的更改,并在数据变脏时更新SQL Server数据库。换句话说,它看起来是这样的:
// Create an instance of your LINQ to SQL DataContext
LINQDataContext dc = new LINQDataContext();
var productQuery = from p in dc.Products select p;
// Create an ObservableCollection to hold your data and bind the DataGrid to this collection
public ObservableCollection<Product> Products {get; set;}
Products = new ObservableCollection<Product>(productQuery.ToList());
public ListCollectionView Products {get; set;}
Products = new ListCollectionView(productQuery.ToList());
希望有帮助