LINQ to SQL and Gridview

7 posts, 0 answers
  1. Donta
    Donta avatar
    2 posts
    Member since:
    Apr 2009

    Posted 16 Apr 2009 Link to this post

    Anyone have any good examples of successfully binding a LINQ result with a parent child relationship to a gridview?

    Thanks, Donta
  2. gerbrand
    gerbrand avatar
    78 posts
    Member since:
    Mar 2009

    Posted 16 Apr 2009 Link to this post

    Hi Donta,

    I don't know if you can link a linq result to the radgridview.DataSource, never try it. But if you have your linq object you can easily readout the linq object and add your rows to the gridview.

    If I have some time today I'm going to try to bind a linq object directly to the gridview.

    Will let you know if it worked.

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Donta Dalpoas
    Donta Dalpoas avatar
    3 posts
    Member since:
    Jul 2006

    Posted 16 Apr 2009 Link to this post

    I can bind the linq result to the grid and the parent level of rows is rendered. How do you set the datasource of the childtemplate to a property of the linq result is the more targeted question.

    Thanks for the reply...Donta
  5. gerbrand
    gerbrand avatar
    78 posts
    Member since:
    Mar 2009

    Posted 16 Apr 2009 Link to this post

    Hi Donta,

    Maybe this can help you?

    1 radGridView1.DataSource = nwindDataSet.Suppliers; //your linq result master/parent 
    2  
    3 GridViewTemplate template = new GridViewTemplate(); 
    4 template.DataSource = nwindDataSet.Products; //your linq result childs 
    5 radGridView1.MasterGridViewTemplate.ChildGridViewTemplates.Add(template); 
    6  
    7 GridViewRelation relation = new GridViewRelation(radGridView1.MasterGridViewTemplate); 
    8 relation.ChildTemplate = template; 
    9 relation.RelationName = "SuppliersProducts"
    10 relation.ParentColumnNames.Add("SupplierID"); 
    11 relation.ChildColumnNames.Add("SupplierID"); 
    12 radGridView1.Relations.Add(relation); 

    This is an example from the help files of the radgridview. Can be found under topic: Hierarchical Grid -> Binding to Hierarchical Data Programmatically.




  6. Donta Dalpoas
    Donta Dalpoas avatar
    3 posts
    Member since:
    Jul 2006

    Posted 16 Apr 2009 Link to this post

    Yes...I've read this. This seems to be designed to handle the situation in which you have two separate "tables" or List<> of objects to use as the datasources. In my case I have an object structured like the following:

    objLINQResult
    Parent
       Child
       Child
       Child
    Parent
       Child
       Child
    Parent
       Child

    I'm binding the objLINQResult to the datasource of the grid but since the children are all members of each parent I have no datasource in the LINQ object to bind to the childtemplate. The hierarchy is already built-in basically.

    Make sense?

    Thanks, Donta
  7. gerbrand
    gerbrand avatar
    78 posts
    Member since:
    Mar 2009

    Posted 16 Apr 2009 Link to this post

    Okay, I had the same situation.

    Try this sample code:

    1 foreach (var item in sortedByLft) 
    2             { 
    3                 if (item.ParentId == 0) //we've got a parent 
    4                 { 
    5                     gridview.MasterGridViewTemplate.Rows.Add(new object[] { item.Id, item.Name }); 
    6                     template = rgvNetwork.MasterGridViewTemplate.ChildGridViewTemplates[0]; 
    7                 } 
    8                 else // we've got or child 
    9                 { 
    10                     if (template == nullcontinue
    11                     template.Rows.Add(new object[] { item.ParentId, item.Id, item.Name}); 
    12                 } 
    13             } 

    the sortedByLft is my linq result
    where I sorted parents and childs

    make sure you made a relationship like this:

    1 gridview.MasterGridViewTemplate.Columns.Add(new GridViewDataColumn { FieldName = "Id", HeaderText = "Id", HeaderTextAlignment = ContentAlignment.MiddleCenter }); 
    2 gridview.MasterGridViewTemplate.Columns.Add(new GridViewDataColumn { FieldName = "Name", HeaderText = "Name", HeaderTextAlignment = ContentAlignment.MiddleCenter }); 
    3  
    4  
    5 var template = new GridViewTemplate { AllowAddNewRow = false, AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill }; 
    6  
    7 template.Columns.Add(new GridViewTextBoxColumn { FieldName = "Id", IsVisible = false }); 
    8 template.Columns.Add(new GridViewTextBoxColumn { FieldName = "childid", HeaderText = "Child id", HeaderTextAlignment = ContentAlignment.MiddleCenter}); 
    9 template.Columns.Add(new GridViewTextBoxColumn { FieldName = "Childname", HeaderText = "Child name", HeaderTextAlignment = ContentAlignment.MiddleCenter}); 
    10  
    11 var relation = new GridViewRelation(gridview.MasterGridViewTemplate) 
    12             { 
    13                 ChildTemplate = template, 
    14                 RelationName = "relationshipID" 
    15             }; 
    16             relation.ParentColumnNames.Add("Id"); 
    17             relation.ChildColumnNames.Add("Id"); 
    18             gridview.Relations.Add(relation); 

    also before every bind of the object clear your rows

    1 gridview.MasterGridViewTemplate.Rows.Clear(); 
    2 gridview.MasterGridViewTemplate.ChildGridViewTemplates.Clear(); 

    I think that should solved it.




  8. Donta Dalpoas
    Donta Dalpoas avatar
    3 posts
    Member since:
    Jul 2006

    Posted 16 Apr 2009 Link to this post

    I'll give it a shot later today...thanks for all the help.

    Donta
Back to Top
UI for WinForms is Visual Studio 2017 Ready