This is a migrated thread and some comments may be shown as answers.

LINQ to SQL and Gridview

6 Answers 279 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Donta
Top achievements
Rank 1
Donta asked on 16 Apr 2009, 05:11 AM
Anyone have any good examples of successfully binding a LINQ result with a parent child relationship to a gridview?

Thanks, Donta

6 Answers, 1 is accepted

Sort by
0
gerbrand
Top achievements
Rank 2
answered on 16 Apr 2009, 09:09 AM
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.

0
Donta Dalpoas
Top achievements
Rank 1
answered on 16 Apr 2009, 01:02 PM
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
0
gerbrand
Top achievements
Rank 2
answered on 16 Apr 2009, 01:11 PM
Hi Donta,

Maybe this can help you?

radGridView1.DataSource = nwindDataSet.Suppliers; //your linq result master/parent 
 
GridViewTemplate template = new GridViewTemplate(); 
template.DataSource = nwindDataSet.Products; //your linq result childs 
radGridView1.MasterGridViewTemplate.ChildGridViewTemplates.Add(template); 
 
GridViewRelation relation = new GridViewRelation(radGridView1.MasterGridViewTemplate); 
relation.ChildTemplate = template; 
relation.RelationName = "SuppliersProducts"
relation.ParentColumnNames.Add("SupplierID"); 
relation.ChildColumnNames.Add("SupplierID"); 
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.




0
Donta Dalpoas
Top achievements
Rank 1
answered on 16 Apr 2009, 01:19 PM
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
0
gerbrand
Top achievements
Rank 2
answered on 16 Apr 2009, 01:31 PM
Okay, I had the same situation.

Try this sample code:

foreach (var item in sortedByLft) 
            { 
                if (item.ParentId == 0) //we've got a parent 
                { 
                    gridview.MasterGridViewTemplate.Rows.Add(new object[] { item.Id, item.Name }); 
                    template = rgvNetwork.MasterGridViewTemplate.ChildGridViewTemplates[0]; 
                } 
                else // we've got or child 
                { 
                    if (template == nullcontinue
                    template.Rows.Add(new object[] { item.ParentId, item.Id, item.Name}); 
                } 
            } 

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

make sure you made a relationship like this:

gridview.MasterGridViewTemplate.Columns.Add(new GridViewDataColumn { FieldName = "Id", HeaderText = "Id", HeaderTextAlignment = ContentAlignment.MiddleCenter }); 
gridview.MasterGridViewTemplate.Columns.Add(new GridViewDataColumn { FieldName = "Name", HeaderText = "Name", HeaderTextAlignment = ContentAlignment.MiddleCenter }); 
 
 
var template = new GridViewTemplate { AllowAddNewRow = false, AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill }; 
 
template.Columns.Add(new GridViewTextBoxColumn { FieldName = "Id", IsVisible = false }); 
template.Columns.Add(new GridViewTextBoxColumn { FieldName = "childid", HeaderText = "Child id", HeaderTextAlignment = ContentAlignment.MiddleCenter}); 
template.Columns.Add(new GridViewTextBoxColumn { FieldName = "Childname", HeaderText = "Child name", HeaderTextAlignment = ContentAlignment.MiddleCenter}); 
 
var relation = new GridViewRelation(gridview.MasterGridViewTemplate) 
            { 
                ChildTemplate = template, 
                RelationName = "relationshipID" 
            }; 
            relation.ParentColumnNames.Add("Id"); 
            relation.ChildColumnNames.Add("Id"); 
            gridview.Relations.Add(relation); 

also before every bind of the object clear your rows

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

I think that should solved it.




0
Donta Dalpoas
Top achievements
Rank 1
answered on 16 Apr 2009, 01:36 PM
I'll give it a shot later today...thanks for all the help.

Donta
Tags
GridView
Asked by
Donta
Top achievements
Rank 1
Answers by
gerbrand
Top achievements
Rank 2
Donta Dalpoas
Top achievements
Rank 1
Share this question
or