DataGridview with DLINQ

Using Visual C# Express in a Windows Application I write with ADO.Net:

SqlDataAdapter sql_adapter = new SqlDataAdapter("Select * from Customers", "Data Source=.\\SQLEXPRESS;AttachDbFilename=northwnd.mdf");

DataSet ds = new DataSet();
sql_adapter.Fill(ds);
dataGridView1.DataSource= ds;
dataGridView1.DataMember="Table";

May somebody translate previous sentences used in a WinForm to show in the datagridview the customers rows, for similar using DLINQ

Thanks in advance

Roberto



Answer this question

DataGridview with DLINQ

  • tejasjunior

    Vittor,

    In conclusion, with your help, I may shown in the datagrid the whole customer list with only the next two sentences :

    private void button10_Click(object sender, EventArgs e)

    {

    var customers = db.Customers.ToList();

    dataGridView1.DataSource= customers;

    }

    The "ToList" append is the one that is giving me the right data.

    In your help you add more functionality for the databind and the creation of new records, that is also very interesting.

    I tried also with db.executequery and in this case it is not working:

    private void button8_Click(object sender, EventArgs e)

    {

    var customers= db.ExecuteQuery<Customer>("Select * from Customers");

    dataGridView1.DataSource=customers;

    }

    Thanks a lot for your help


  • Dhinesh

    Thanks for your replies, Vitor.

    In your sample I do not see the "games" definition.

    May you extend it


  • timeee

    The better way would be to implement an IDataSource.  There are a couple approaches to this.  Google and/or stay tuned -- this is one pairing we're working on a solution for.

    ### UPDATE

    Excuse me, I misread -- the above would apply for ASP.NET.



  • recruz

    I found a second way more "dlinqly" using one collection instead a datatable:

    {
    var cli_s =
    from c in db.Customers
    orderby c.CustomerID
    select c;

    Coleccion Clientes = new Coleccion();
    foreach (var cl in cli_s)
    Clientes.Add((Customer)cl);
    dataGridView1.DataSource=Clientes;
    }

    public class Coleccion:System.Collections.CollectionBase{

    public Customer this[int Index] {
    get{
    return ((Customer)List[Index]);
    }
    set{
    List[Index] = value;
    }
    }

    public int Add(Customer value) {
    return List.Add(value);
    }
    }


  • Ben Donley

    Note that ExecuteQuery<T> returns an IEnumerable<T>. You should therefore be able to call ToList() on it, which is an extension method for IEnumerable<T>.

    See http://msdn2.microsoft.com/en-us/library/k39d6s23.aspx



  • Kanna

    Colleccion is the Collection class that inherits Collection base.

    Customer is the Customers class as in samples:

    public class Northwind : DataContext

    {

    public Table<Customer> Customers;

    public Northwind(string connection): base(connection) {}

    }

    [Table(Name="Customers")]

    public class Customer

    {

    private string _City;

    private string _CustomerID;

    [Column(Id=true)]

    //public string CustomerID;

    public string CustomerID {

    get {

    return this._CustomerID;

    }

    set {

    if ((this._CustomerID != value)) {

    //this.OnChanging();

    this._CustomerID = value;

    }

    }

    }

    [Column]

    //public string City;

    public string City {

    get {

    return this._City;

    }

    set {

    if ((this._City != value)) {

    //this.OnChanging();

    this._City = value;

    }

    }

    }

    }


  • Foxabilo

    I alawys can use one intermediate datatable, but I think Dlinq must do it in other way:

    var cust =

    from c in db.Customers

    where c.City == "Madrid"

    select c;

    DataTable dt =new DataTable ();

    dt.Columns.Add("CustomerID");

    dt.Columns.Add("City");

    foreach (var cli in cust)

    dt.Rows.Add( cli.CustomerID,cli.City);

    dataGridView1.DataSource=dt;


  • Somnath

    Hello Roberto.

    Just for suggestion for you, I recently working with this duet and I found in System.ComponentModel.BindingList<T> class the better solution to link they.

    Vitor



  • Apsis

    Roberto, BindingList<T> class is generic and so strongly typed solution:

    var customers = db.Customers.

    Where(c => c.City == "Madrid").

    ToList();

    var customersBinding = new BindingList<Customer>(customers);

    // DataGrid will raise this event on row added

    customersBinding.AddingNew += delegate(object sender, AddingNewEventArgs e)

    {

    Customer customer = new Customer();

    e.NewObject = customer;

    };

    // Use this event to perform changes in database

    customersBinding.ListChanged += delegate(object sender, ListChangedEventArgs e)

    {

    // example:

    if (e.ListChangedType == ListChangedType.ItemAdded)

    {

    Customer customer = customersBinding[e.NewIndex];

    db.Customers.Add(customer);

    }

    // make code also to remove and if you want to update

    };

    dataGrid.DataSource = games;

    // don't forget of call .SubmitChanges() method on datacontext

    Vitor



  • btrabon

    DLinq team is still working on data binding story; but because DataGridView works on IList you can do the following for Northwind database

    db = new Northwind(ConnectionString);

    Customers = db.GetTable<Customer>();

    private void Form1_Load(object sender, System.EventArgs e)

    {

    var q = from c in Customers where c.City == "London" select c;

    var b = q.ToList()

    this.bindingSource1.DataSource = b; //In this case I've put a BindingSource on my form that I've used as the DataSource for the DataGridView -- so dataGridView.DataSource = bindingSource1.

    You will still need to do some work with Grid events to handle add row and remove row appropriately and will need a Save button that calls db.SubmitChanges();



  • oshea00

    Hello people!

    using System.Query;

    var customers= db.ExecuteQuery<Customer>("Select * from Customers").ToList();

    Roberto. Realy if you not interesting in change data, a simple List solve the problem.

    Vitor



  • Padvit

    I agree.

    But I do not know how to add "ToList" to the ExecuteQuery sentence.

    I tried several ways without success.

    Do you know how to write:

    var customers= db.ExecuteQuery<Customer>("Select * from Customers");

    adding the ToList method


  • John Paul Fullerton

    Roberto

    Excuse me! is not "games", is "customers". ("games" was of my own code :))

    Is Colleccion your class What interface/class you inherited

    Vitor



  • DataGridview with DLINQ