Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Fill a DataGridView with data from 2 related tables

Fill a DataGridView with data from 2 related tables

Scheduled Pinned Locked Moved C#
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aecordoba
    wrote on last edited by
    #1

    Hi, there! I have two related tables in a DataSet and I want to fill a DataGridView with data from both tables. Could anybody tell me an easy way to do it? Thank you, in advance.

    -- Adrián Córdoba

    R I A 3 Replies Last reply
    0
    • A aecordoba

      Hi, there! I have two related tables in a DataSet and I want to fill a DataGridView with data from both tables. Could anybody tell me an easy way to do it? Thank you, in advance.

      -- Adrián Córdoba

      R Offline
      R Offline
      RepliCrux
      wrote on last edited by
      #2

      1. You can create a third table and insert the required data in that table and then bind the datasource to that table. 2. Or you can write a store procedure which will just get those data and return select.

      1 Reply Last reply
      0
      • A aecordoba

        Hi, there! I have two related tables in a DataSet and I want to fill a DataGridView with data from both tables. Could anybody tell me an easy way to do it? Thank you, in advance.

        -- Adrián Córdoba

        I Offline
        I Offline
        I explore code
        wrote on last edited by
        #3

        You can create a third data table (dt3) at runtime in the same dataset (ds), populate it with the fields from the two data tables(dt1 and dt2) and then finally bind this data table with the grid view using its data source property as shown below (but this solution is effective only if there will be same number and type of fields everytime you create and populate a new data table): dataGridView1.DataSource=ds.Tables["dt3"]; This should solve your problem

        A 1 Reply Last reply
        0
        • I I explore code

          You can create a third data table (dt3) at runtime in the same dataset (ds), populate it with the fields from the two data tables(dt1 and dt2) and then finally bind this data table with the grid view using its data source property as shown below (but this solution is effective only if there will be same number and type of fields everytime you create and populate a new data table): dataGridView1.DataSource=ds.Tables["dt3"]; This should solve your problem

          A Offline
          A Offline
          aecordoba
          wrote on last edited by
          #4

          Thank you! I'll try that way.

          -- Adrián Córdoba

          1 Reply Last reply
          0
          • A aecordoba

            Hi, there! I have two related tables in a DataSet and I want to fill a DataGridView with data from both tables. Could anybody tell me an easy way to do it? Thank you, in advance.

            -- Adrián Córdoba

            A Offline
            A Offline
            aecordoba
            wrote on last edited by
            #5

            Hi, fellows! When I was trying the solutions you recommended to me, I found a third way to solve my problem (and I think it work so good). 1. I created a DataGridView and I chose as Data Source, the Binding Source of the first table. 2. I added unbound columns to the DataGridView according to the second table. 3. I added a handler for DataBindingComplete event of the DataGridView. 4. I added the following code in that handler in order to fill the cells corresponding to the second table: foreach (DataGridViewRow listRow in listDataGridView.Rows) { int addressId = (int)listRow.Cells[2].Value; CompaniesDataSet.AddressesRow addressRow = companiesDataSet.Addresses.FindByAddressId(addressId); listRow.Cells[4].Value = addressRow.Address; listRow.Cells[5].Value = addressRow.City; listRow.Cells[6].Value = addressRow.ZIPCode; listRow.Cells[7].Value = addressRow.State; } (I hope you can understand my explanation because my English is not so good.) Thank you very much, again.

            -- Adrián Córdoba

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups