Master-detail grid with 2 dataTable (C# & DevExpress)
<p>I try to programmatically code 2 dataTable into Master-Detail grid. Unfortunately is been a week, I try to google the solution and i still fail. 
 I using c# and DevExpress, I manage to set the additional Level1 as gridView2.
The main is the gridview1 and all which is under gridContorl1. Below is the code I did, and seeking advise hope i manage proceed this final step of my experiment program.
The main table is tableSalesOrder &amp; the detail tale is tableDetail. How to relation the tableSalesOrder and tableDetail, when select the tableSalesOrder ItemCode, the tableDetail will only show the ItemCode under Master-Detail view grid</p>

<pre><code>    private void btnLoadSO_Click(object sender, EventArgs e)
    {
        LoadOutstandingSO();
        LoadDetailDS();
        gridControl1.DataSource = tableSalesOrder;
    }

   private void LoadOutstandingSO()
    {
        if (tableSalesOrder == null)
            tableSalesOrder = new DataTable("dtSO");
        else
            tableSalesOrder.Clear();

        string sqlSelect2a = "select 0 AS [Check], k.ItemCode, k.[Data Count], l2.Description, l2.ItemGroup, l2.ItemType,"
            + " CASE WHEN l2.CostingMethod = 0 THEN 'Fixed Cost' WHEN l2.CostingMethod=1 THEN 'Weighted Average'"
            + " WHEN l2.CostingMethod=2 THEN 'F.I.F.O' WHEN l2.CostingMethod=3 THEN 'L.I.F.O' ELSE 'Most Recently' END AS [CostingMethod],"
            + " k.UOM, k.Location, K.[Purchased Qty],K.[Sold Qty], K.[Adj Qty], K.[Total Qty], l.BalQty, l2.Desc2, l2.Discontinued, l2.IsActive"
            + " from (Select g.ItemCode, count(*) as [Data Count], g.UOM, g.Location,"
            + " Sum(g.InQty) as [Purchased Qty], Sum(g.OutQty) as [Sold Qty], SUM(g.AdjQty) as [Adj Qty],"
            + " Sum((g.InQty - g.OutQty) + g.AdjQty) as [Total Qty]"
            + " From (Select ivd.ItemCode as ItemCode, ivd.UOM, ivd.Location, 0 as [InQty], ivd.Qty as [OutQty], 0 as [AdjQty]"
            + " From IV ak Join IVDtl ivd on ak.DocKey = ivd.DocKey where ak.Cancelled = 'F'";
        string sqlSelect3a = " Union All"
            + " Select csd.ItemCode as ItemCode, csd.UOM, csd.Location, 0 as [InQty], csd.Qty as [OutQty], 0 as [AdjQty]"
            + " From CS ak Join CSDtl csd on ak.DocKey = csd.DocKey where ak.Cancelled = 'F'";
        string sqlSelect4a = " Union All"
            + " Select pid.ItemCode as ItemCode, pid.UOM, pid.Location, pid.Qty as [InQty], 0 as [OutQty], 0 as [AdjQty]"
            + " From PI ak Join PIDtl pid on ak.DocKey = pid.DocKey where ak.Cancelled = 'F'";
        string sqlSelect5a = " Union All"
            + " Select cpd.ItemCode as ItemCode, cpd.UOM, cpd.Location, cpd.Qty as [InQty], 0 as [OutQty], 0 as [AdjQty]"
            + " From CP ak Join CPDtl cpd on ak.DocKey = cpd.DocKey where ak.Cancelled = 'F'";
        string sqlSelect6a = " Union All"
            + " Select adjd.ItemCode as ItemCode, adjd.UOM, adjd.Location, 0 as [InQty], 0 as [OutQty], adjd.Qty as [AdjQty]"
            + " From ADJ ak Join ADJDTL adjd on ak.DocKey = adjd.DocKey where ak.Cancelled = 'F'";
        string sqlSelect2b = ") g join Item z on g.ItemCode=z.ItemCode where z.StockControl='T'";
        string sqlSelect2c = " Group By g.ItemCode, g.UOM, g.Location) k"
                        + " join ItemBalQty l on k.ItemCode=l.ItemCode and k.UOM=l.UOM and k.Location=l.Location"
                        + " join Item l2 on k.ItemCode=l2.ItemCode";

        System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(mydbset.ConnectionString);
        System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand();
        sqlCmd.Connection = conn;

        BCE.AutoCount.SearchFilter.SearchCriteria crit1a = new BCE.AutoCount.SearchFilter.SearchCriteria();
        BCE.AutoCount.SearchFilter.SearchCriteria crit1b = new BCE.AutoCount.SearchFilter.SearchCriteria();
        crit1a.AddFilter(ucDate.Filter);
        crit1b.AddFilter(ucItem.Filter);

        string critSQL1a = crit1a.BuildSQL(sqlCmd);
        string critSQL1b = crit1b.BuildSQL(sqlCmd);

        if (critSQL1a != "")
            sqlSelect2a += " AND " + critSQL1a + sqlSelect3a +" AND " + critSQL1a
                    + sqlSelect4a + " AND " + critSQL1a + sqlSelect5a + " AND " + critSQL1a
                    + sqlSelect6a + " AND " + critSQL1a + sqlSelect2b;
        else
            sqlSelect2a += sqlSelect3a + sqlSelect4a + sqlSelect5a + sqlSelect6a + sqlSelect2b;

        if (critSQL1b != "")
            sqlSelect2a += " AND " + critSQL1b + sqlSelect2c;
        else
            sqlSelect2a += sqlSelect2c;

        string sqlSelect = sqlSelect2a;

        sqlCmd.CommandText = sqlSelect + " ORDER BY ItemCode";

        System.Data.SqlClient.SqlDataAdapter adpt = new System.Data.SqlClient.SqlDataAdapter(sqlCmd);

        try
        {
            adpt.Fill(tableSalesOrder);
        }
        catch (BCE.Application.AppException ex)
        {
            throw ex;
        }
    }

    private void LoadDetailDS()
    {
        if (tableDetail == null)
            tableDetail = new DataTable("dtDT");
        else
            tableDetail.Clear();

        string sqlSelect2be = "select ak.ItemCode, ak.UOM, ak.Location, ak.DocDate, ak.DocNo, ak.Qty, ak.UnitPrice from"
            + " (select g.ItemCode, g.UOM, g.Location, ak.DocDate, ak.DocNo, g.Qty, g.UnitPrice"
            + " from PI ak right join PIDTL g on ak.DocKey=g.DocKey"; 
        string sqlSelect3be = " union all"
            + " select g.ItemCode, g.UOM, g.Location, ak.DocDate, ak.DocNo, g.Qty , g.UnitPrice"
            + " from CP ak right join CPDTL g on ak.DocKey=g.DocKey";
        string sqlSelect4be = ") ak JOIN Item z on z.ItemCode=ak.ItemCode where z.StockControl='T'";


        System.Data.SqlClient.SqlConnection conn2 = new System.Data.SqlClient.SqlConnection(mydbset.ConnectionString);
        System.Data.SqlClient.SqlCommand sqlCmd2 = new System.Data.SqlClient.SqlCommand();
        sqlCmd2.Connection = conn2;

        BCE.AutoCount.SearchFilter.SearchCriteria crit2be = new BCE.AutoCount.SearchFilter.SearchCriteria();
        BCE.AutoCount.SearchFilter.SearchCriteria crit3be = new BCE.AutoCount.SearchFilter.SearchCriteria();
        crit2be.AddFilter(ucDate.Filter);
        crit3be.AddFilter(ucItem.Filter);

        string critSQL2be = crit2be.BuildSQL(sqlCmd2); 
        string critSQL3be = crit3be.BuildSQL(sqlCmd2);

        if (critSQL3be != "")
            sqlSelect2be += " WHERE " + critSQL3be + sqlSelect3be + " WHERE " + critSQL3be + sqlSelect4be;
        else
            sqlSelect2be += sqlSelect3be + sqlSelect4be;


        if (critSQL2be != "")
            sqlSelect2be += " AND " + critSQL2be;

        sqlCmd2.CommandText = sqlSelect2be + " order by ItemCode, DocDate, DocNo";

        System.Data.SqlClient.SqlDataAdapter adpt = new System.Data.SqlClient.SqlDataAdapter(sqlCmd2);

        try
        {
            adpt.Fill(tableDetail);
        }
        catch (BCE.Application.AppException ex)
        {
            throw ex;
        }

    }
</code></pre>