none
Binding to a DatagridView locks cells?? RRS feed

  • Question

  • I have bound a Linq query to a dataGridView and now I cannot change the cells in the Grid, is this what I should expect when bound to linq?

     

    I have pasted my query in cast there is a problem with that

    var Di = from d in db.TblDishItems

    join iids in db.TblLiqItems on d.IID equals iids.LiqId

    join Sids in db.TblSuppliers on iids.SupplierId equals Sids.Supplierid

    where d.RId == Rid

    select new

    {

    d.Itemid,

    d.IID,

    d.BinNo,

    iids.LiqItemName,

    d.RQuant,

    d.UseageUnit,

    d.ScaledQuant,

    d.ScaledUnit,

    d.UseIN

     

    };

    gv.DataSource = Di;

     

    Wednesday, August 6, 2008 4:36 PM

Answers

  • In C#, Anonymous types are immutable. If you want an editable grid, you need to project the full object. You'll probably need to use a comboboxcolumn or the LiqItemName to handle that relationship.

     

    Jim Wooley

    www.ThinqLinq.com

    Wednesday, August 6, 2008 5:29 PM
    Moderator
  • There are several implementations of converting a LINQ result to a datatable. Do a quick search on linq to datatable and you should be able to find them. Typically they are fairly reflection heavy.

     

    Jim Wooley

    www.ThinqLinq.com

     

    Wednesday, August 6, 2008 6:06 PM
    Moderator

All replies

  • In C#, Anonymous types are immutable. If you want an editable grid, you need to project the full object. You'll probably need to use a comboboxcolumn or the LiqItemName to handle that relationship.

     

    Jim Wooley

    www.ThinqLinq.com

    Wednesday, August 6, 2008 5:29 PM
    Moderator
  • not what I wanted to hear but thanks anyway

     

    also I wanted to copy my linq query result to a DataTable, I take it that is  not possible

     

    Wednesday, August 6, 2008 5:34 PM
  • There are several implementations of converting a LINQ result to a datatable. Do a quick search on linq to datatable and you should be able to find them. Typically they are fairly reflection heavy.

     

    Jim Wooley

    www.ThinqLinq.com

     

    Wednesday, August 6, 2008 6:06 PM
    Moderator
  • Thanks

     

    Wednesday, August 6, 2008 6:28 PM
  • I picked up a simple solution from http://www.entityspaces.net/blog/2008/05/24/EntitySpaces+2008+LINQ+To+DataTable.aspx

     

    as you said there are many options but this one does not match the way you suggest in your book so my question now is, is there anything wrong or disadvantageous in using this method?

     

    As you can see I can also bind to the datagridview so that I can edit the grid.

    I did have to add "MultipleActiveResultSets=True" to my connection string

    As far as I can see all it does is pass the linq generated SQL into the Adapter

     

    var Di = from d in db.TblDishItems

    join iids in db.TblLiqItems on d.IID equals iids.LiqId

    join Sids in db.TblSuppliers on iids.SupplierId equals Sids.Supplierid

    where d.RId == Rid

    select new

    {

    d.Itemid,

    d.IID,

    d.BinNo,

    iids.LiqItemName,

    d.RQuant,

    d.UseageUnit,

    d.ScaledQuant,

    d.ScaledUnit,

    Cost = (d.RQuant * iids.LiqItemCost) * iids.Tblunit.BValue,

    d.UseIN,

    };

    SqlCommand LinqCmd = db.GetCommand(Di) as SqlCommand;

    DataTable dt = new DataTable();

    SqlDataAdapter dBadapter = new SqlDataAdapter(LinqCmd);

    dBadapter.Fill(dt);

    gv.DataSource = dt;

     

     

    Thursday, August 7, 2008 4:31 PM