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
  1. Home
  2. General Programming
  3. C#
  4. How to add data from multiple table to datagrid in C#

How to add data from multiple table to datagrid in C#

Scheduled Pinned Locked Moved C#
helpcsharpcssdatabasetutorial
2 Posts 2 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
    aashish saalvi
    wrote on last edited by
    #1

    Hi i am building an application in which i want to generate a report. That report consists of data from multiple tables. I tried doing this but when the datagrid is populated with the dataset/datatable the record from second table comes on second row & from third table comes on third row. I am using access database. can someone help me out of this. Thanks in advance. the code is as shown below. private void Form1_Load(object sender, EventArgs e) { try { con = new OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=e:/ReportTest/ReportTest/ContractSystemDB.mdb"); con.Open(); ds = new DataSet(); dt = new DataTable("ashish"); cmd = new OleDbCommand("select * from CommonData",con); rd = cmd.ExecuteReader(); if (rd.HasRows) { while (rd.Read()) { da = new OleDbDataAdapter("select * from InitialApproval where InitialApproval.PrNo='"+rd.GetString(0)+"'",con); da.Fill(ds); da.Dispose(); da1 = new OleDbDataAdapter("select EstimatedVal,FwdDtByPSD from PSDApproval where PSDApproval.PrNo='" + rd.GetString(0) + "'",con); da1.Fill(ds); da1.Dispose(); da2 = new OleDbDataAdapter("select POValue,rcd_dtCC,saving from CommonData where CommonData.PrNo='" + rd.GetString(0) + "'", con); da2.Fill(ds); da2.Dispose(); } } contractSystemDBDataSetBindingSource.DataSource = ds; MyGrid.DataSource = contractSystemDBDataSetBindingSource.DataSource; MessageBox.Show("Record Added To Grid View"); } catch (Exception ex) { MessageBox.Show("Error==>" + ex); } finally { con.Close(); } }

    J 1 Reply Last reply
    0
    • A aashish saalvi

      Hi i am building an application in which i want to generate a report. That report consists of data from multiple tables. I tried doing this but when the datagrid is populated with the dataset/datatable the record from second table comes on second row & from third table comes on third row. I am using access database. can someone help me out of this. Thanks in advance. the code is as shown below. private void Form1_Load(object sender, EventArgs e) { try { con = new OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=e:/ReportTest/ReportTest/ContractSystemDB.mdb"); con.Open(); ds = new DataSet(); dt = new DataTable("ashish"); cmd = new OleDbCommand("select * from CommonData",con); rd = cmd.ExecuteReader(); if (rd.HasRows) { while (rd.Read()) { da = new OleDbDataAdapter("select * from InitialApproval where InitialApproval.PrNo='"+rd.GetString(0)+"'",con); da.Fill(ds); da.Dispose(); da1 = new OleDbDataAdapter("select EstimatedVal,FwdDtByPSD from PSDApproval where PSDApproval.PrNo='" + rd.GetString(0) + "'",con); da1.Fill(ds); da1.Dispose(); da2 = new OleDbDataAdapter("select POValue,rcd_dtCC,saving from CommonData where CommonData.PrNo='" + rd.GetString(0) + "'", con); da2.Fill(ds); da2.Dispose(); } } contractSystemDBDataSetBindingSource.DataSource = ds; MyGrid.DataSource = contractSystemDBDataSetBindingSource.DataSource; MessageBox.Show("Record Added To Grid View"); } catch (Exception ex) { MessageBox.Show("Error==>" + ex); } finally { con.Close(); } }

      J Offline
      J Offline
      jaypatel512
      wrote on last edited by
      #2

      Your question is not so clear . .but as much i can understand you want to show all the tables columns in one data grid... For that you cannot use such a code but u may need to use UNION in your query. So you need to select all the data in single query using union and then populate data ina single table. You can google union .. you will get it . .. It should be something like this : da1 = new OleDbDataAdapter("select * from InitialApproval where InitialApproval.PrNo='"+rd.GetString(0)+"' UNION select EstimatedVal,FwdDtByPSD from PSDApproval where PSDApproval.PrNo='" + rd.GetString(0) + "' UNION select POValue,rcd_dtCC,saving from CommonData where CommonData.PrNo='" + rd.GetString(0) + "'", con); da.Fill(ds);

      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