windowsforms-4057-common-features-data-binding-binding-to-linq-to-sql-classes.md
Follow the steps below to bind your data-aware control to data using the LINQ to SQL provider.
Latest Visual Studio versions (for example, Visual Studio 2017) do not have required modules installed by default. For these versions, you have to install required tools manually. To do so, open Windows Start Menu and run “Visual Studio Installer”.
Select “Modify” for the required Visual Studio installation, switch to “Individual components” tab and check the “LINQ to SQL tools” option.
Click the “Modify” button to start the installation. After the IDE is updated, close the Installer.
Open the Visual Studio Server Browser and right-click “Data Connections” to add a new connection.
Invoke a Data Source Configuration Wizard and select the “LINQ to SQL” option.
Click “New Data Source…” button and add a new “LINQ to SQL Classes” item to your project.
Data classes are created and edited in an Object Relational Designer (O/R Designer). To create a data class, drag it from the Server Explorer onto the O/R Designer’s surface.
Rebuild the project and close the O/R Designer.
Invoke a Data Source Configuration Wizard once again and select the “LINQ to SQL” option. This time the data source you have created is listed among available data sources. Select it and click “Next”.
Choose the required data binding mode.
The final wizard page allows you to sort your data descending or ascending by a selected data field.
Click “Finish” and rebuild the solution. Your data-aware control is now bound to LINQ-to-SQL classes.
Call the SubmitChanges method to save changes back to a data-aware control’s underlying data source.
//direct binding
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
(gridControl1.DataSource as Table<Order>).Context.SubmitChanges();
}
//binding with a BindingSource component
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
(ordersBindingSource.DataSource as Table<Order>).Context.SubmitChanges();
}
'direct binding
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
TryCast(gridControl1.DataSource, Table(Of Order)).Context.SubmitChanges()
End Sub
'binding with a BindingSource component
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
TryCast(ordersBindingSource.DataSource, Table(Of Order)).Context.SubmitChanges()
End Sub