How to add data from multiple table to datagrid in C#
-
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(); } }
-
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(); } }
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);