returning a table + LINQ

  • Thread starter Thread starter Sampat
  • Start date Start date
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
 
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.
 
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
 
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?
 
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?
 
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?
 
Back
Top