Server Mode with LINQ to SQL
- 3 minutes to read
The PivotGridControl automatically operates in server mode if it is bound to the LINQ to SQL data source. This example shows how to use LINQ to SQL in the Pivot Grid application.
Creating Data Classes
Add LINQ to SQL Classes to the project.
Data classes can then be created and edited in an Object Relational Designer (O/R Designer). An O/R Designer provides a visual design surface for creating LINQ to SQL entity classes and relationships based on objects in a database. To learn more, see the Microsoft LINQ to SQL article.
You can create and map entity classes to tables and views by dragging database tables and views from Server Explorer onto the O/R Designer.
Save your changes, close the O/R Designer, and rebuild the solution.
Binding a Pivot Grid Control to the LinqServerModeSource component
Drag the LinqServerModeSource component and drop it onto the Form.
Note
Alternatively, you can create the LinqServerModeSource in code at runtime.
Specify the type of objects retrieved from a data source with the LinqServerModeSource.ElementType and LinqServerModeSource.KeyExpression properties:
Specify the queryable source using the LinqServerModeSource.QueryableSource property:
Bind the Pivot Grid control to the LinqServerModeSource component:
The resulting code looks as follows:
using DevExpress.Data.Linq; using DevExpress.XtraEditors; using System; namespace LinqToSqlServerModeExample { public partial class Form1 : DevExpress.XtraEditors.XtraForm { LinqServerModeSource linqServerModeSource1; bool serverMode = false; public Form1() { InitializeComponent(); // Create LINQ Server Mode data source. linqServerModeSource1 = new LinqServerModeSource { ElementType = typeof(Invoice), KeyExpression = "OrderID" }; // Create the data context and enable query logging. NWindDataContext dc = new NWindDataContext { Log = Console.Out }; // // Specify the queryable source that provides data items. linqServerModeSource1.QueryableSource = dc.Invoices; } private void Form1_Load(object sender, EventArgs e) { pivotGridControl1.DataSource = linqServerModeSource1; } } }
Run the project. The PivotGridControl works in server mode because it is bound to the LINQ-to-SQL data source. You can see the generated SQL statements in the Visual Studio Output window.
Note
The complete sample project Pivot Grid LINQ to SQL Server Mode Example is available in the DevExpress Examples repository.