returning a table + LINQ

S

Sampat

Hi,
I wanted to know how we can return the whole table using linq and
use that table to bind to a DataGridView in Win Forms. Below is the
code sample which I am trying but get a compile error as we cannot
return var. Any code sammple would really help.


public var GetAllProducts()
{
DataContext db = new DataContext();
var products = from p in db.Products
select p;
return products;
}

-Thanks
 
S

Sampat

Hi,
   I wanted to know how we can return the whole table using linq and
use that table to bind to a DataGridView in Win Forms. Below is the
code sample which I am trying but get a compile error as we cannot
return var. Any code sammple would really help.

 public var GetAllProducts()
        {
            DataContext db = new DataContext();
            var products = from p in db.Products
                           select p;
            return products;
        }

-Thanks

Also, is there a sample which shows how to update data using
DataGridView + LINQ + Datbabase.
 
C

Cor Ligthert[MVP]

You mean something like this

private void Form1_Load(object sender, EventArgs e)
{
FillDataGridView(bindingSource1);
dataGridView1.DataSource = (bindingSource1);
}
private void FillDataGridView(BindingSource bindingSource)
{
NorthwindDataContext dc = new NorthwindDataContext();
bindingSource.DataSource = dc.Employees;
}

Cor
 
S

Sampat

You mean something like this

private void Form1_Load(object sender, EventArgs e)
 {
      FillDataGridView(bindingSource1);
      dataGridView1.DataSource = (bindingSource1);
 }
 private void FillDataGridView(BindingSource bindingSource)
{
      NorthwindDataContext dc = new NorthwindDataContext();
      bindingSource.DataSource = dc.Employees;

}

Cor

Thanks..and how does editing gridview data from UI update the object
and then internally the database table?
 
C

Cor Ligthert[MVP]

Sampat,

That is very easy,

\\\
NorthwindDataContext dc = (NorthwindDataContext)
((System.Data.Linq.Table<Employee>)bindingSource1.DataSource).Context
;
dc.SubmitChanges();
///

Cor


"Sampat" <[email protected]> schreef in bericht
You mean something like this

private void Form1_Load(object sender, EventArgs e)
{
FillDataGridView(bindingSource1);
dataGridView1.DataSource = (bindingSource1);
}
private void FillDataGridView(BindingSource bindingSource)
{
NorthwindDataContext dc = new NorthwindDataContext();
bindingSource.DataSource = dc.Employees;

}

Cor

Thanks..and how does editing gridview data from UI update the object
and then internally the database table?
 
C

Cor Ligthert[MVP]

Second attempt.

Sampat,

That is very easy,

\\\
NorthwindDataContext dc = (NorthwindDataContext)
((System.Data.Linq.Table<Employee>)bindingSource1.DataSource).Context
;
dc.SubmitChanges();
///

Cor


"Sampat" <[email protected]> schreef in bericht
You mean something like this

private void Form1_Load(object sender, EventArgs e)
{
FillDataGridView(bindingSource1);
dataGridView1.DataSource = (bindingSource1);
}
private void FillDataGridView(BindingSource bindingSource)
{
NorthwindDataContext dc = new NorthwindDataContext();
bindingSource.DataSource = dc.Employees;

}

Cor

Thanks..and how does editing gridview data from UI update the object
and then internally the database table?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top