Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to use a DatagridView with Linq to SQL

I have never used datagrids and such, but today I came across a simple problem and decided to "databind" stuff to finish this faster, however I've found that it doesn't work as I was expecting.

I though that by doing something as simple as:

 var q = from cust in dc.Customers
         where cust.FirstName == someString
         select cust;

 var list = new BindingList<Customer>(q.ToList());
 return list;

Then using that list in a DataGridView1.DataSource was all that I needed, however, no matter how much I google, I can't find a decent example on how to populate (for add/edit/modify) the results of a single table query into a DataGridView1. Most samples talk about ASP.NET which I lack, this is WinForms.

Any ideas?

I've came across other posts and the GetNewBindingList, but that doesn't seem to change much.

What am I missing (must be obvious)?

like image 945
Martin Marconcini Avatar asked Jun 16 '09 11:06

Martin Marconcini


People also ask

Is LINQ to SQL still used?

LINQ to SQL was the first object-relational mapping technology released by Microsoft. It works well in basic scenarios and continues to be supported in Visual Studio, but it's no longer under active development.

Is LINQ converted to SQL?

LINQ to SQL translates the queries you write into equivalent SQL queries and sends them to the server for processing. More specifically, your application uses the LINQ to SQL API to request query execution. The LINQ to SQL provider then transforms the query into SQL text and delegates execution to the ADO provider.

How does LINQ to SQL work?

In LINQ to SQL, the data model of a relational database is mapped to an object model expressed in the programming language of the developer. When the application runs, LINQ to SQL translates into SQL the language-integrated queries in the object model and sends them to the database for execution.

Does LINQ improve performance?

LINQ syntax is typically less efficient than a foreach loop. It's good to be aware of any performance tradeoff that might occur when you use LINQ to improve the readability of your code. And if you'd like to measure the performance difference, you can use a tool like BenchmarkDotNet to do so.


1 Answers

You can just bind the IQueryable result to the DataGridView, not sure why you converting it to a BindingList, is there a specific reason for that?

like image 61
Lazarus Avatar answered Sep 22 '22 10:09

Lazarus