Friday, November 24, 2023

c#, devexpress - How to make master-detail relation on non-DB objects (List generic )

List<TmpParent> list = new List<TmpParent>();

List<TmpChild> listChildExternal = new List<TmpChild>();
Initial data filling:
var p = new TmpParent( "A", "A", true );
p.listInner.Add( new TmpChild() );
list.Add( p );
list.Add( new TmpParent( "B", "B", false, TypeEnum.OPERATOR ) );
list.Add( new TmpParent( "C", "C", true ) );
list.Add( new TmpParent( "D", "D", false ) );

listChildExternal.Add( new TmpChild() { str1="C", str2 = "child1" } );
listChildExternal.Add( new TmpChild() { str1="C", str2 = "child1.1" } );
listChildExternal.Add( new TmpChild() { str1="B", str2 = "child1.2" } );

gridParent.DataSource = list;	// master
Find and show detail data:
  • First detail list for inner list.
  • Second detail list for external list connected with relation.
private void gridViewParent_FocusedRowChanged( object sender, 
  DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e ) {
   
  if ( e.FocusedRowHandle >= 0 ) {        
    TmpParent master = gridView1.GetRow( e.FocusedRowHandle ) as TmpParent;
    if ( master != null ) {          
      /* A] try to find in inner list */
      gridDetail.DataSource = master.listInner;

      /* B] try to find in external list */           
      gridChildExternal.DataSource = 
        listChildExternal.Where( x => x.str1 == master.str1 ).ToList();
    }
  }
}
First grid=master, second grid=detail internal list, third grid=detail external list.

No comments:

Post a Comment