Home Forums WinForms controls Xceed Grid for WinForms hierarchical data binding to business objects in master/detail grid

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #16499 |

    I’ve searched and searched the forums, documentation, and sample code for an answer to this, and I’ve picked up a clue here and there, but have yet to find a complete answer to this.

    I have a three-level business object hierarchy that I’m trying to represent in a master/detail grid. Here’s the basic idea:

    – Bank
    |—- Branch
          |——- Transaction

    I can see from the documentation that it is possible to do this if my DataSource is in a database. Then the relevant data bindings would be something like:

    gridControl.SetDataBinding( dbDataSet, “Banks” );
    detailGridTemplate.SetDataBinding( dbDataSet, “Banks.Branches” );
    detailGridTemplate2.SetDataBinding( dbDataSet, “Banks.Branches.Transactions” );

    However, I AM NOT using a database for this data. (It’s not actually banks, I just used that for simplicity. The data I’m using is generated dynamically.)  I can do a two-level hierarchy with this code:

    gridControl.BeginInit();
    gridControl.SetDataBinding( banks, “” );

    DetailGrid branchGrid = new DetailGrid();
    gridControl.DetailGridTemplates.Add( branchGrid );
    branchGrid.SetDataBinding( null, “Branches” );
    branchGrid.HeaderRows.Add(new ColumnManagerRow());

    gridControl.EndInit();

    That appears to work just fine, but when I try to add the third level, Transactions, like this:

    gridControl.BeginInit();
    gridControl.SetDataBinding( banks, “” );

    DetailGrid branchGrid = new DetailGrid();
    gridControl.DetailGridTemplates.Add( branchGrid );
    branchGrid.SetDataBinding( null, “Branches” );
    branchGrid.HeaderRows.Add(new ColumnManagerRow());

    DetailGrid transactionGrid = new DetailGrid();
    gridControl.DetailGridTemplates.Add( transactionGrid );
    transactionGrid.SetDataBinding( null, “Branches.Transactions” );
    transactionGrid.HeaderRows.Add(new ColumnManagerRow());

    gridControl.EndInit();

    I get an exception from EndInit() saying the dataSource property may not be null.

    Thanks in advance

    Imported from legacy forums. Posted by ageer (had 1332 views)

    User (Old forums)
    Member
    Post count: 23064

    What type of data are you using?  How do you establish the relationship between your levels?  When you say the data is generated dynamically, do you use a DataSet in which you feed your data?

    Imported from legacy forums. Posted by André (had 305 views)

    User (Old forums)
    Member
    Post count: 23064

    The objects have string properties which are displayed and then some other properties with the [Browsable(false)] attribute which aren’t displayed.  Then the two upper-level classes each have a generic list.  Something like the following, to continue with the Bank paradigm:

    public class Bank
    {
      readonly string name;
      readonly List<Branch> branches;

      public string Name
      {
        get { return name; }
      }

      public List<Branch> Branches
      {
        get { return branches; }
      }

      public Bank(string name, List<Branch> branches)
      {
        this.name = name;
        this.branches = branches;
      }  
    }

    public class Branch
    {
      readonly string city;
      readonly List<Transaction> transactions;

      public string City
      {
        get { return city; }
      }

      public List<Transaction> Transactions
      {
        get { return transactions; }
      }

      public Branch(string city, List<Transaction> transactions)
      {
        this.city = city;
        this.transactions = transactions;
      }
    }

    public class Transaction
    {
      readonly string amount;

      public string Amount
      {
        get { return amount; }
      }

      public Transaction(int amount)
      {
        this.amount = amount.ToString();
      }
    }

    at the top level:

    List<Bank> banks;

    and you already saw the code I’m using to try to bind to the grid. I do not use a DataSet when generating the data.  Establishing the relationship between the levels is precisely what I’m trying to figure out – at least in regards to relationships more than 2 levels deep.

    Imported from legacy forums. Posted by ageer (had 712 views)

    User (Old forums)
    Member
    Post count: 23064

    First, I didn’t notice one mistake you make in adding the second level DetailGrid.  You add it on the master grid, whereas you need to add it to the first level DetailGrid.

    gridControl.DetailGridTemplates.Add( transactionGrid );

    should be :

    branchGrid.DetailGridTemplates.Add( transactionGrid );

     

    Second, since you don’t have a relationship, you need to set the binding directly to the property name, in this case Transaction.

    transactionGrid.SetDataBinding( null, “Branches.Transactions” );

    should be :

    transactionGrid.SetDataBinding( null, “Transactions” );

     

    Imported from legacy forums. Posted by André (had 308 views)

    User (Old forums)
    Member
    Post count: 23064

    That works, thank you André!

    Imported from legacy forums. Posted by ageer (had 1763 views)

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.