Sie sind auf Seite 1von 4

Binding Source For DataGridView From Linq To Sql Query

Mike McIntyre's .NET Journal

Home

Syndication

RSS for Posts Atom RSS for Comments Email Notifications
Your Email Ad

Go

Recent Posts

Windows 8 Modern Line of Business Applications Windows 8 Series Part 3 - Windows RT Windows 8 Series Part 2 - Four Versions of Windows 8 Windows 8 Series - Part 1 New MSDN Developer Tools Blog

Tags

.net

automation

Beginner
collection initializer

extension method

join

lambda

Linq
Metro

Multiline

Programming

silverlight
silverlight 3

sql
String

Tutorial
vb.net

visual basic
visual basic 10 visual basic 2008 visual basic 2010
Visual Studio

Visual Studio 2010


Windows 8
windows forms

View more

Archives

September 2012 (2) August 2012 (2) May 2012 (2) November 2011 (1) September 2011 (6) August 2011 (2) July 2011 (2)

November 2010 (12) October 2010 (2) September 2010 (3) August 2010 (8) June 2010 (3) May 2010 (1) March 2010 (4) February 2010 (6) December 2009 (2) November 2009 (7) October 2009 (9) September 2009 (10) August 2009 (8) July 2009 (3) June 2009 (1) May 2009 (2) April 2009 (3) March 2009 (10) February 2009 (11) January 2009 (5) December 2008 (5) November 2008 (9) September 2008 (1)

LINQ to SQL translates LINQ queries to SQL for execution on a database. The results are strongly typed IEnumerable. Because these objects are ordinary common language runtime (CLR) objects, ordinary object data binding can be used to display the results. On the other hand, sorting and change operations (inserts, updates, and deletes) require additional steps. For example, to provide a DataSource for a DataGridView that can be sorted using the DataGridView's built in sorting capabilities, you can not assisign a LINQ TO SQL query directly to the DataGridView's DataSource.
Instead, assign a LINQ TO SQL query to a BindingSource and then assign the BindingSource to a DataGridView. Visual Basic Example

Friend Shared Function CreditCardTypesList(ByVal activeFilter As Boolean, ByVal titleFilter As String) As BindingSource Dim newBindingSource As New BindingSource() newBindingSource.DataSource = _ From e In DB.VYDal.CreditCardTypes _ Where e.Active = activeFilter And SqlMethods.Like(e.Title, titleFilter & "%") _ Order By e.Title _

Select e Return newBindingSource End Function Bind the Binding Source to a DataGridView:

Private Sub LoadGridView() entityGridView.DataSource = DB.CreditCardTypesList(True, titleFilterTextBox.Text) End Sub C# Example internal static BindingSource CreditCardTypesList(bool activeFilter, string titleFilter) { BindingSource bs = new BindingSource(); bs.DataSource = from e in DB.VYDal.CreditCardTypes where e.Active == activeFilter & SqlMethods.Like(e.Title, titleFilter + "%") orderby e.Title select e; return bs; }
Bind the BindingSource to a DataGridView:

private void LoadGridView() { entityGridView.DataSource = DB.CreditCardTypesList(true, titleFilterTextBox.Text); }


-----------

Das könnte Ihnen auch gefallen