roncansan
roncansan

Reputation: 2380

Load a DataGridView using Linq to SQL

How can I use Linq-To-SQL to load a DataGridView?

Would the following work?

DCDataContext db = new DCDataContext();
dataGridViewJobs.DataSource = db.jobs.Select(p => p.closeDate <= DateTime.Now);

Upvotes: 0

Views: 7553

Answers (2)

MRG
MRG

Reputation: 3219

Winforms :

Alternately You can use BindingSource and DataGridView. You can give your Linq to SQL entity as DataSource to BindingSOurce. BindingSource will work as Datasource to DataGridView.

bindingSource1.DataSource = items;
dataGridView1.DataSource  = bindingSource1;

You can find how To here. alt text

ASP.Net :

Yes it will work. Please check this CodeProject article for step by step How to.

alt text

Upvotes: 2

Ra&#250;l Roa
Ra&#250;l Roa

Reputation: 12396

Yes. But you should change your query, since select is not used for querying like in sql but for projecting the elements of a sequence into a new form.

The following example should work:

   DCDataContext db = new DCDataContext();
    dataGridViewJobs.DataSource = (from jobs in db.jobs
                                    where p.closeDate <= DateTime.Now
                                    select jobs);

Upvotes: 1

Related Questions