Version

Binding WinGrid to Data Using LINQ to SQL

The .NET Language Integrated Query (LINQ) is a querying programming model that can be used with a data source.

You can use LINQ within your programming language, such as C#, to retrieve data and easily manipulate the results. LINQ to SQL is an object relational model implementation that allows you to model a relational database using .NET classes.

This topic will walk you through the process of binding your WinGrid to data using LINQ to SQL.

Before You Begin

Visual Studio 2008 should be installed on your machine.

What You Will Accomplish

This topic will show you how to bind your WinGrid control to data using the LINQ to SQL technology available with Visual Studio 2008.

Follow these Steps

  1. From the toolbox, drag the UltraGrid control to your form.

  2. From the Quick Start Designer, click Finish.

  3. From the Data Sources tab, select Add New Data Source…​

data sources pane in visual studio
  1. The Data Source Configuration Wizard appears.

  2. Select Database, click Next.

data source configuration wizard
  1. In the Choose Your Data Connection dialog box,click New Connection. The Add Connection dialog box appears. To use the LINQ to SQL feature, you must use an SQL Server database.

  2. Change the Data Source to Microsoft SQL Server Database File.

  3. Browse to your SQL Server database.

  4. Click OK.

add connection dialog box
  1. In the Save the Connection String to the Application Configuration File, click Next.

  2. In the message box that appears, click Yes.

make data local to project
  1. Select the Tables node, then click Finish.

  2. On the Project menu, click Add New Item…​

  3. In the Add New Item Dialog box, select LINQ to SQL Classes from the Templates window.

  4. Set Name to NorthWind.dbml

add new item dialog box
  1. Drag the following item from the Server Explorer on to the design surface:

    • Product

    • Category

binding ultragrid to data using linq to sql
  1. On the File menu, click Save. This will persist the .NET classes that represent the entities and database relationships that you created.

  2. In the Form1_Load event enter the following code:

In Visual Basic:

'Create an instance of the NorthWindDataContext
Dim db As New NorthWindDataContext
'Select all products in the database where the category name is Beverages
Dim products = From p In db.Products _
    Where p.Category.CategoryName = "Beverages" _
    Select New With {p.ProductID, p.ProductName}
'Bind WinGrid to the results
Me.UltraGrid1.DataSource = products

In C#:

//Create an instance of the NorthWindDataContext
NorthWindDataContext db = new NorthWindDataContext();
//Select all products in the database where the category name is Beverages
var products = from p in db.Products
where p.Category.CategoryName == "Beverages"
select new
{
  Id = p.ProductId,
  Name = p.ProductName
};
//Bind WinGrid to the results
this.ultraGrid1.DataSource = products;