DataGridView binding to a Many to many relation

Posts   
 
    
Posts: 9
Joined: 15-Jan-2014
# Posted on: 28-Jan-2014 10:37:42   

Using 4.1 selfserving.

Probably I’m searching with the wrong terms. Because I think the questions is easy but I cannot find it in the documentation.

I have a many to many relation between Product and Description. (See attachment)

table Product Id table Description Id

table ProductDescription ProductId DescriptionId

Now I want all the descriptions for a product in a DataGridView (dgvTest) on a Product page.

The code I use (not working!!! confused ) seems rather complex. I think there must be an easy way. Someone has some hint(s) or a solution for me?



private void btnTest_Click(object sender, EventArgs e) {
         dgvTest.DataSource = null;
         ProductDescriptionCollection productDescriptionCollection = _product.GetMultiProductDescriptions(true);

         FieldCompareValuePredicate productfilter = (FieldCompareValuePredicate)(ProductFields.Id == _product.Id);

         DescriptionCollection descriptionCollection = new DescriptionCollection();
         ISortExpression descriptionSorter = new SortExpression(DescriptionFields.Rank | SortOperator.Ascending);

//From Sample....
         RelationCollection relationsToWalk = new RelationCollection();

        
         relationsToWalk.Add(DescriptionEntity.Relations.ProductDescriptionEntityUsingDescriptionId);
         relationsToWalk.Add(ProductDescriptionEntity.Relations.DescriptionEntityUsingDescriptionId);
         relationsToWalk.Add(ProductEntity.Relations.ProductDescriptionEntityUsingProductId);

         descriptionCollection.GetMulti(_productFilter, 0, descriptionSorter, _relationsToWalk);
         dgvTest.DataSource = descriptionCollection;
}




Attachments
Filename File size Added on Approval
Relation.GIF 28,951 28-Jan-2014 10:38.11 Approved
Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 28-Jan-2014 18:57:14   

You did not explain, what "doesn't work" mean? Hint: first thing to do is to examine the generated SQL query, this should give you an idea of what's going on.

Comments on your code:

ProductDescriptionCollection productDescriptionCollection = _product.GetMultiProductDescriptions(true);

Why are you fetching this? I think this line is not needed at all.

You have added some relations twice.

Please try the following:

        var productfilter = new PredicateExpression(ProductFields.Id == _product.Id);

         DescriptionCollection descriptionCollection = new DescriptionCollection();
         ISortExpression descriptionSorter = new SortExpression(DescriptionFields.Rank | SortOperator.Ascending);

//From Sample....
         RelationCollection relationsToWalk = new RelationCollection();

        
         relationsToWalk.Add(DescriptionEntity.Relations.ProductDescriptionEntityUsingDescriptionId);
         relationsToWalk.Add(ProductDescriptionEntity.Relations.ProductEntityUsingProductId);

         descriptionCollection.GetMulti(_productFilter, 0, descriptionSorter, _relationsToWalk);
         dgvTest.DataSource = descriptionCollection;
Posts: 9
Joined: 15-Jan-2014
# Posted on: 29-Jan-2014 08:27:19   

Great is works smile .

Thanx a lot!

case closed.....

The code now is:


         DescriptionCollection descriptionCollection = new DescriptionCollection();
         var productfilter = new PredicateExpression(ProductFields.Id == _product.Id);
         ISortExpression descriptionSorter = new SortExpression(DescriptionFields.Rank | SortOperator.Ascending);

         //From Sample....
         RelationCollection relationsToWalk = new RelationCollection();

         relationsToWalk.Add(DescriptionEntity.Relations.ProductDescriptionEntityUsingDescriptionId);
         relationsToWalk.Add(ProductDescriptionEntity.Relations.ProductEntityUsingProductId);

         descriptionCollection.GetMulti(productfilter, 0, descriptionSorter, relationsToWalk);
         dgvTest.DataSource = descriptionCollection;

ps: The code you quoted -> I was using it because i was trying (and error) to get results....in short I did not know what to do flushed