Results 1 to 8 of 8

Thread: How to display fields from multiple datatable in a datagrid

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    40

    Question How to display fields from multiple datatable in a datagrid

    Hi,

    I have a problem and would be happy if you guys out there can help me out with this.

    Let say I have 2 tables. One of them is "Customer" and another is "State".

    The "Customer" table has a field which is linked to the "State" table using relationship.

    Usually in VB6, I would open a recordset using the "JOIN" statement and bind it to a datagrid.

    In VB .NET, if I don't want to use the "JOIN" statement but to open the 2 tables in a dataset, how do I display the data correctly in a datagrid?

    To make the picture clear, so here goes,

    *** Data in the 2 tables ***

    Customer Table
    --------------------
    CustomerID: 1
    Name: Peter
    StateID: 1

    CustomerID: 2
    Name: John
    StateID: 2


    State Table
    --------------
    StateID: 1
    Name: Virginia

    StateID: 2
    Name: Ohio


    *** The result I wanted to be displayed in the datagrid ***

    Code:
    +----------+----------+
    | Name     | State    |
    +----------+----------+
    | Peter    | Virginia |
    | John     | Ohio     |
    +----------+----------+
    Thanks in advance

  2. #2
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327
    Use an SQL query:
    SELECT *
    FROM Customer, State
    WHERE Customer.StateID = State.StateID

    Then, use the result from this query to fill your datagrid.
    I think there should be a query builder in the IDE, if you want to fool around with the query a little bit. In principle, you could save this query and link it as the data source to the DBGrid at design time.
    -scott
    he he he

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    40
    By doing this, the record is not updatable. If I want to use dataset, is there any way to do so?

    Thanks.

  4. #4
    New Member
    Join Date
    Mar 2002
    Location
    Wesy Sussex, England
    Posts
    4
    You need a Data Relation object.

    You can build one in code (I think there is a sample in the on line help) or you can do it visually thus:

    Create the dataset so that it contains both tables

    Double click on the dataset's .xsd file in Solution Explorer and you will get a view of both tables on screen.

    From the toolbox drag a relation object onto the window containing the tables and drop it on the child table.

    Follow you nose from there on, it should be straightforward.

    Cheers
    Last edited by blackcat; Mar 29th, 2002 at 04:50 PM.

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    40
    Ok, we're going somewhere. Creating the Data Relation object is not a problem. I did actually tried it out before but.... I still can't map data from different tables to the same row of the datagrid.


    Thanks.

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    40

    Wink

    I manage to solve the problem. But please, anyone out there confirm if my solution is valid.

    Set the SelectCommand for the SqlDataAdapter to,
    Code:
    SELECT Customer.CustomerID, Customer.Name, Customer.StateID, State.Name AS State
    FROM Customer LEFT OUTER JOIN State ON Customer.StateID= State.StateID
    Set the UpdateCommand for the SqlDataAdapter to,
    Code:
     
    UPDATE Customer
    	SET Name = @Name, StateID = @StateID
    WHERE
    	(CustomerID = @Original_CustomerID);
    SELECT Customer.CustomerID, Customer.Name, Customer.StateID, State.State AS State
    FROM Customer LEFT OUTER JOIN State ON Customer.StateID= State.StateID
    WHERE (CustomerID = @CustomerID)
    How do I get all those SQL statements above? Well, this is what I did.
    1) Create 2 SqlDataAdapters named SqlDataAdapter1 and SqlDataAdapter2.
    2) Configure SqlDataAdapter1 using its wizard to select the whole Customer table.
    3) Configure SqlDataAdapter2 using its wizard to select the 2 tables.
    4) Copy the SelectCommand from SqlDataAdapter2
    5) Paste it into SqlDataAdapter1's SelectCommand
    6) Edit SqlDataAdapter1's UpdateCommand and replace the SELECT statement with the one from step (4)
    7) Repeat step (6) for SqlDataAdapter1's InsertCommand
    8) Delete SqlDataAdapter2
    9) Generate dataset.

    By doing so, I get what I wanted and the dataset can be updated.

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    40

    Thumbs down

    I've been thinking... if I use the code above... then what is the purpose of dataset?

    I thought by using dataset, we don't need to use SQL statement "JOIN" anymore...

    Anyone out there have any idea?

    Thanks.

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    40

    Talking

    I finally solved my problem. I found a user control in the forum here "DropDownColumn.zip". I actually forgot which thread it is and from where I downloaded the file.

    Anyway, after modifying several lines, I am able to get what I want without using the JOIN statement in the SQL statement.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width