0

I want to display data in a DataGridView control, this data will take its values from 3 tables (boq_table, submittal, summary) such as;


  • itemNum || descriptionOfWork || unit || contractualQuantity || priceNum from boq_table.

  • executedQuantLastSummary || priceLastWorks || executedQuantBetw2Sum from summary.

  • priceCurrentWorks from submittal.


And there are two columns that should fill after the calculation process in two fields displayed in DataGrid view is it possible to make this calculation automatically after fill data in dataGridView or should it be before filling?

I am trying to use Full outer in code below but it doesn't work.

adapter = new MySqlDataAdapter("SELECT ubc.boq_table.itemNum AS '1', ubc.boq_table.descriptionOfWork AS '2', ubc.boq_table.unit AS '3', ubc.boq_table.contractualQuantity AS '4', ubc.boq_table.priceNum AS '5' , ubc.summary.executedQuantLastSummary AS '6', ubc.summary.priceLastWorks AS '7 ', ubc.summary.executedQuantBetw2Sum AS '8' , ubc.submittal.priceCurrentWorks AS '9 '  from ubc.boq_table FULL OUTER JOIN ubc.summary ON ubc.boq_table.itemNum = ubc.submittal.itemNum = ubc.summary.itemNum where projectName='" + projectNameText.Text + "' And itemNum= '" + textBox3.Text + "' ", connection);

            table = new DataTable();
            adapter.Fill(table);
            dataGridView1.DataSource = table;
Muhammad Usman Bashir
  • 1,441
  • 2
  • 14
  • 43
Sarah
  • 9
  • 5
  • Did you try using the query directly in MySQL before doing it in c#? Also please define "*it doesn't work*". (btw, using numbers for column names is quite odd - `AS '1'`) – Rafalon Jan 27 '20 at 12:55

1 Answers1

0

To make full outer join on 3 tables

SELECT t1.value, t2.value, t3.value
FROM t1 LEFT JOIN t2 ON t1.value = t2.value
        LEFT JOIN t3 ON t1.value = t3.value
UNION ALL
SELECT t1.value, t2.value, t3.value
FROM t2 LEFT JOIN t1 ON t1.value = t2.value
        LEFT JOIN t3 ON t2.value = t3.value
WHERE t1.value IS NULL
UNION ALL
SELECT t1.value, t2.value, t3.value
FROM t3 LEFT JOIN t1 ON t1.value = t3.value
        LEFT JOIN t2 ON t2.value = t3.value
WHERE t1.value IS NULL AND t2.value IS NULL

As an alternative for this:

SELECT t1.value, t2.value, t3.value
FROM t1 FULL OUTER JOIN t2 ON t1.value = t2.value
        FULL OUTER JOIN t3 ON t1.value = t3.value

Reference from SO post :Full outer MySQL

And to load data to datatable :

public void LoadData()
{
    DataTable NewDataTable = new DataTable();

    string FullOuterQuery = "Your reworked query";
    MySqlDataAdapter MySQLDA= new MySqlDataAdapter(FullOuterQuery, connection);
    MySQLDA.Fill(NewDataTable);
    MySQLDA.Dispose();
    dataGridView1.DataSource = NewDataTable;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kuba Do
  • 155
  • 9
  • please can you explain to me why you type "IS NULL"? I need query with exactly where condition I typed – Sarah Jan 29 '20 at 12:50
  • You can change that, it was copy/pasted from another post, but this is exactly what You need. – Kuba Do Feb 03 '20 at 09:25