Link to home
Start Free TrialLog in
Avatar of shieldguy
shieldguyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How to filter records in a dataset having 2 tables with Parent and Child Relationship

I have a dataset that contain 2 tables.

Table 1 = Child Table
Table 2 = Master Table

The dataset tables has got the Parent Child relationship using the following code:

ds.Relations.Add(ParentColumn, ChildColumn);

What I want to achieve here is the filtered records using a criteria. For example the records of only the employees (Master Table) which are currently working in the company and not the leavers. To do that I need to use one of the columns in the master table but the records from the child table will be used to display on the form.

I hope I am clear explaining the problem.

Could anybody help please?

Avatar of x77
x77
Flag of Spain image

The Datasource of DataGridView is a DataView (DefaulltView if you use a DataTable)


       
    DataSet Ds = new DataSet();
    DataGridView DataGridViewMaster, DataGridViewDetail;
    BindingSource BsMaster, BsDetail;
 
    private void Form1_Load(object sender, EventArgs e)
    {   
        //Rellenar Dataset....
 
        DataTable  TMaster = Ds.Tables["Master"],
                   TDetail = Ds.Tables["Detail"];
        //Build Relation
        DataRelation RMD = 
          new DataRelation("MasterDetail",
             new DataColumn[]{TMaster.Columns ["KeyCode"] },
             new DataColumn[]{TDetail.Columns ["KeyCode"] },true );
 
        BsMaster = new BindingSource (this.components );
        BsMaster.DataSource = new DataView( TMaster, 
            @"KeyCode like 'A%'", 
            null,DataViewRowState.CurrentRows );
        BsDetail = new BindingSource (BsMaster ,RMD.RelationName );
        this.components.Add(BsDetail );
 
        DataGridViewMaster.DataSource =BsMaster ;
        DataGridViewDetail.DataSource =BsDetail ;
 
        // To Change Filter:
        DataGridViewMaster.EndEdit ();
        DataGridViewDetail.EndEdit ();
        // BsMaster.DataSource = new DataView( TMaster,  @"Key....
        

Open in new window

Avatar of shieldguy

ASKER

Thanks for the code. I tried using this code but it doesn't recognize 'this.components'. I have no clue what is this components keyword. Anyways, Please have a look at the following code which I am trying to use and let me know the solution accordingly. Just to let you know that I am not using the native grid of dot net but the Infragistics ultra web grid which takes a table as it's datasource. There is no difference between binding a datasource to the native dot net grid and the ultra grid. Both take datatable as their datasource.

DataSet dsPers;
dsPers = oEmpPers.GetDataSetAsc(); //This returns the records of the child table
dsPers.Merge(dsEmpMstr.Tables[0]);  //This returns the records of the master table

dsPers.Relations.Add(dsPers.Tables[1].Columns[0], dsPers.Tables[0].Columns[0]); //This creates the
                                                                                                                                    //relation between
                                                                                                                      //the two tables of the dataset.

Finally I want to show the records from the child table of the dataset but by using a filter on a field called 'enable' which is present int he master table. i.e. 'enable = Y' which means to show only the employees which are enabled. The datasource of the grid is assigned by using the following line of code:

UGrid.DataSource = dsPers.Table[0]; //Table 0 is the child table.


You Can ignore components -  New BindingSource()
delete - this.components.Add(BsDetail );

I use it for Dispose when form disposes.
Whe I add a BindingSource whith the Ide, Generate code includes Components.
private System.ComponentModel.IContainer components;
 
[DebuggerStepThrough]
private void InitializeComponent()
{
 
  this.components = new System.ComponentModel.Container();
  ....
  
  [DebuggerNonUserCode]
protected override void Dispose(bool disposing)
{
    try
    {
        if (((disposing && (this.components != null)) ? 1 : 0) != 0)
        {
            this.components.Dispose();
        }
    }
    finally
    {
        base.Dispose(disposing);
    }
}
 
 

Open in new window

I am sorry but I dont' understand what you are trying to say here. Can you be a bit more clear please. I am not an expert in dot net but only a beginer
ASKER CERTIFIED SOLUTION
Avatar of x77
x77
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial