locked
Entity framework and datagridview RRS feed

  • Question

  • Hello !

    i'm using entity framework , and i have a datagridview bound with  entity.table1.

    Table1                   Table2

    ID1                         ID2

    Name                     statusname

    City                        statusNumber

                                  ID1

    Table1-Table2 : 1-to-1

    In datagridview i want to display all the columns of table1 and the column statusnumber from table2.

    Datagridview

    ID1        Name         City         StatusNumber

     

    i have created datagridview ,  only with columns of table1 , but i don't know how to add the column from table2.

     

    Thank You.

    Friday, November 25, 2011 12:24 AM

Answers

  • On 11/24/2011 8:33 PM, dcode25 wrote:
    > i have selected the datasource for datagridview on design
    >
    > And all the column from first table are bound to textbox on
    > datagridview.Now how to bind the column from other table to a textbox on
    > datagridview ?
    >
     
    Whether a FormLoad or PageLoad, I suggest you set the Datasource at
    runtime and have all your objects in a collection of objects, which
    would happen if you use the method of doing the query I am talking about
    and binding that way.
     
    All you doing in your way is binding at design time and object on the
    Model, which happens to be a collection of objects from one table.
     
     
    The way I am showing you is to use the Join with a Select New Shape  and
    shape the object with the properties wanted from the two tables with the
    result being a collection of the objects with needed properties that can
    be bound to the control.
     
    datagridview can be set to use the properties of the new object's
    properties.
        
    • Marked as answer by Alan_chen Thursday, December 29, 2011 6:23 AM
    Friday, November 25, 2011 2:24 AM

All replies

  • On 11/24/2011 7:24 PM, dcode25 wrote:
    > Hello !
    >
    > i'm using entity framework , and i have a datagridview bound with
    > entity.table1.
    >
    > Table1 Table2
    >
    > ID1 ID2
    >
    > Name statusname
    >
    > City statusNumber
    >
    > ID1
    >
    > Table1-Table2 : 1-to-1
    >
    > In datagridview i want to display all the columns of table1 and the
    > column statusnumber from table2.
    >
    > Datagridview
    >
    > ID1 Name City StatusNumber
    >
    > i have created datagridview , only with columns of table1 , but i don't
    > know how to add the column from table2.
    >
    > Thank You.
    >
     
    Do a join on the two table and do a 'new' Select New Shape. The results
    will be an output of table1 and table2 properties.
     var q = from c in customers
                join o in orders on c.Key equals o.Key
                select new {c.Name, o.OrderNumber};
     
     
    Friday, November 25, 2011 1:10 AM
  •  
    Oh, the results are bindable to the control.
     
    Friday, November 25, 2011 1:20 AM
  • i have selected the datasource for datagridview on design

    And all the column from first table are bound to textbox on datagridview.Now how to bind the column from other table to a textbox on datagridview ?

    Friday, November 25, 2011 1:33 AM
  • On 11/24/2011 8:33 PM, dcode25 wrote:
    > i have selected the datasource for datagridview on design
    >
    > And all the column from first table are bound to textbox on
    > datagridview.Now how to bind the column from other table to a textbox on
    > datagridview ?
    >
     
    Whether a FormLoad or PageLoad, I suggest you set the Datasource at
    runtime and have all your objects in a collection of objects, which
    would happen if you use the method of doing the query I am talking about
    and binding that way.
     
    All you doing in your way is binding at design time and object on the
    Model, which happens to be a collection of objects from one table.
     
     
    The way I am showing you is to use the Join with a Select New Shape  and
    shape the object with the properties wanted from the two tables with the
    result being a collection of the objects with needed properties that can
    be bound to the control.
     
    datagridview can be set to use the properties of the new object's
    properties.
        
    • Marked as answer by Alan_chen Thursday, December 29, 2011 6:23 AM
    Friday, November 25, 2011 2:24 AM