Binding Master Detail data to XtraReport Winforms DevExpress Programmatically

3.9k views Asked by At

Am trying to bind data (Master Details) to XtraReport programmatic. If Master is single data/values means XtraReport working fine. But in my case Master is multiple data/values, here all Details data is prints on all Masters.

eg. (Vehicle Report) - Here 1)Cars & 2)Bikes are Master, Details of Cars Master is BMW, Datsun, Hyundai. Details of Bike Master is Honda, Moto, Yamaha. Now I want to print like this

Report

Cars
   BMW
   Datsun
   Hyundai

Bikes
   Honda
   Moto
   Yamaha

but it printing like this

Report 

Cars
   BMW
   Datsun
   Hyundai
   Honda
   Moto
   Yamaha

Bikes
   BMW
   Datsun
   Hyundai
   Honda
   Moto
   Yamaha

Already I place Labels in designer XtraReports. This is my code to prints data on XtraReports programmatic

private void Report1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
    {
        try
        {
            StrConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Properties.Settings.Default.DataPath + @"CMDB\database.mdb";

            string qry2 = "SELECT chequeid AS ChqBukId, chdescri AS ChqBukDesc, (SELECT bankname FROM tblBankMaster WHERE bank_id=tbchequebook.bankID) AS BankName FROM tbchequebook";
            OleDbDataAdapter adapter1 = new OleDbDataAdapter(qry2, StrConnection);
            DataSet ds1 = new DataSet();
            adapter1.Fill(ds1, "Detail");
            ds1.Tables[0].TableName = "QryBank";
            DetailReport.DataSource = ds1;

            XRBinding bindingBankName = new XRBinding("Text", ds1, "QryBank.BankName");
            TXE_BankName.DataBindings.Add(bindingBankName);
            XRBinding bindingchqbukame = new XRBinding("Text", ds1, "QryBank.ChqBukDesc");
            TXE_ChequeBookDesc.DataBindings.Add(bindingchqbukame);

            string qry4 = "SELECT chq_no, chq_date, party_name, chq_amount FROM tblchequeleafnew WHERE Status='P' ";

            OleDbDataAdapter adapter3 = new OleDbDataAdapter(qry4, StrConnection);
            DataSet ds3 = new DataSet();
            adapter3.Fill(ds3, "Detail2");
            ds3.Tables[0].TableName = "QryCheque";
            DetailReport1.DataSource = ds3;

            XRBinding bindingChqNo = new XRBinding("Text", ds3, "QryCheque.chq_no");
            TXE_ChequeNo.DataBindings.Add(bindingChqNo);

            XRBinding bindingChqDate = new XRBinding("Text", ds3, "QryCheque.chq_date", "{0:MM/dd/yyyy}");
            TXE_ChequeDate.DataBindings.Add(bindingChqDate);

            XRBinding bindingPartyName = new XRBinding("Text", ds3, "QryCheque.party_name");
            TXE_PartyName.DataBindings.Add(bindingPartyName);

            XRBinding bindingChqAmt = new XRBinding("Text", ds3, "QryCheque.chq_amount", "{0:N2}");
            TXE_ChequeAmount.DataBindings.Add(bindingChqAmt);

        }
        catch (Exception ex)
        {
            XtraMessagebox.Show(ex.message.tostring());
        }
    }

This is my XtraReport

XtraReport

How to solve this to get exact Master & Its Details. Using this above code am getting Master exactly, but it show all details under all masters.

1

There are 1 answers

0
nempoBu4 On

You need to set up relations between your master and details levels.
If you are using DataSet then you need to use one DataSet object for all DataSource members and DataSet.Relations collection to create the relations between your tables. Also you need to use the relation name as DetailReport.DataMember property and use the same relation name in bindings.
Here is example: Report design

private void XtraReport1_BeforePrint(object sender, PrintEventArgs e)
{
    var vehicleTypes = new DataTable("VehicleTypes");
    vehicleTypes.Columns.Add("Type", typeof(string));

    vehicleTypes.Rows.Add("Cars");
    vehicleTypes.Rows.Add("Bikes");

    var vehicles = new DataTable("Vehicles");
    vehicles.Columns.Add("Type", typeof(string));
    vehicles.Columns.Add("Name", typeof(string));

    vehicles.Rows.Add("Cars", "BMW");
    vehicles.Rows.Add("Cars", "Datsun");
    vehicles.Rows.Add("Cars", "Hyundai");
    vehicles.Rows.Add("Bikes", "Honda");
    vehicles.Rows.Add("Bikes", "Moto");
    vehicles.Rows.Add("Bikes", "Yamaha");

    var dataSet = new DataSet("Data");

    dataSet.Tables.Add(vehicleTypes);
    dataSet.Tables.Add(vehicles);
    dataSet.Relations.Add("VehiclesRelation", vehicleTypes.Columns["Type"], vehicles.Columns["Type"]);

    DataSource = dataSet;

    vehicleTypeLabel.DataBindings.Add("Text", dataSet, "Type");

    VehicleDetailReport.DataMember = "VehiclesRelation";
    vehicleLabel.DataBindings.Add("Text", dataSet, "VehiclesRelation.Name");
}