Populating WPF datagrid using Linq To Sql class.
Linq To Sql class is used for retrieving data from sql server in a much easier way.To populate a datagrid in WPF,it becomes very simple if we use Linq to Sql
The following steps give the detailed description of how to use Linq to sql class in WPF projects.
1. Add Linq To Sql Class to our WPF project
2. In Tools =>Connect To Database,
• Add the required details like server name, authentication, database etc. and test the connection.
3. In Server Explorer you can see the database and its tables, from there drag and drop the required table, view to dbml design surface.
4. In the MainWindow.xaml page
• Create an instance for the dbml data context
LinqSampleDataContext objSample = new LinqSampleDataContext();
(Note : We can get the datacontext name from dbml properties)
• Perform Linq select query
var result = from rslt in objSample.Tablename
• Bind the result to datagrid
DataGrid1.ItemsSource = result.ToList();