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

using linq vs tableAdapters for hierarchical relationships

4 Answers 110 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 2
Joe asked on 05 Jan 2018, 04:26 PM

Hello --

I am using the following to populate a datagrid and to setup hierarchical relationships for a parent/child grid:

 

private void radButton1_Click(object sender, EventArgs e)
        {
 
            FlexEDIEntities db = new FlexEDIEntities();
 
            var gridData = (from d in db.billing_transactions
                            where d.status == 1
                            select new
                            {
                                d.Id,
                                d.stop_details.con_name,
                                d.stop_details.con_address1,
                                d.stop_details.con_city,
                                d.weight_billed,
                                d.base_amount,
                               count =  d.billing_transaction_accessorial_charge.Count
                            }).ToList();
 
 
            main_grid.DataSource = gridData;
            if (this.main_grid.Relations.Count == 0) // does not exist yet
            {
                
            GridViewRelation relation = new GridViewRelation(this.main_grid.MasterTemplate);
            relation.ChildTemplate = accessorial_template;
            relation.RelationName = "TransToCharges";
            relation.ParentColumnNames.Add("Id");
            relation.ChildColumnNames.Add("billing_transaction_id");
            this.main_grid.Relations.Add(relation);
 
            }
        }

 

I have a lot of these to do in this project so I am trying to not have to have a bunch of extra data sources in the project.

It is not necessary that the datagrid update the underlying data source as edits will be handled by dedicated screens.
Can I set up the child data source using a linq query, either by modifying the one above or having a second one that is dedicated for the child relationship?

 

Thank you --

 

Joe

4 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 08 Jan 2018, 10:45 AM
Hi Joe,

Thank you for writing.

You should be able to use LINQ and perform queries on your data source and then bind the grid to the returned data. Please note, however, that if you return anonymous objects as in the shared code snippet you would not be able to edit the grid. The standard .NET DataGridView control behaves the same way.

In order to have a fully functional grid, I can suggest performing the LINQ queries and to return a type from them. You can check the following resources:
Additionally, you can also check the following documentation article demonstrating how Entity Framework can be used with the grid: https://docs.telerik.com/devtools/winforms/gridview/populating-with-data/binding-to-entityframework-using-database-first-approach.

I hope this helps. Let me know if you need further assistance.

Regards,
Hristo
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Joe
Top achievements
Rank 2
answered on 08 Jan 2018, 01:18 PM

Thank you,

 

I will work through the example and give it a try.

I have no problem bind data from a linq query to a datatable, just setting up the parent/child.

I appreciate the point in the right direction and will up date this post later.

 

--Joe

0
Joe
Top achievements
Rank 2
answered on 08 Jan 2018, 01:28 PM

OK...not quite.

Following the example assumes that I want to load the entire table.

What I am trying to do is first have a linq query that returns a subset of the table.

Then add the relationship and childtemplate *without* having to add a new datasource.

Here is the complete code that is working, but using a datasource called flexEDIDataSetBindingSource:

 

transactions_grid.DataSource = gridData;
//setup the accessorial charges template for the child table
GridViewTemplate accessorial_template = new GridViewTemplate();
accessorial_template.AllowAddNewRow = false;
accessorial_template.Columns.Add(new GridViewTextBoxColumn("description"));
accessorial_template.Columns.Add(new GridViewTextBoxColumn("amount"));
accessorial_template.DataSource = flexEDIDataSetBindingSource;
accessorial_template.Caption = "Accessorial Charges";
 
GridViewTextBoxColumn transIDcolumn = new GridViewTextBoxColumn("billing_transaction_id");
transIDcolumn.IsVisible = false;
accessorial_template.Columns.Add(transIDcolumn);
 
accessorial_template.BestFitColumns();
 
transactions_grid.MasterTemplate.Templates.Add(accessorial_template);
 
if (this.transactions_grid.Relations.Count == 0) // does not exist yet
{
 
    GridViewRelation relation = new GridViewRelation(this.transactions_grid.MasterTemplate);
    relation.ChildTemplate = accessorial_template;
    relation.RelationName = "TransToCharges";
    relation.ParentColumnNames.Add("Id");
    relation.ChildColumnNames.Add("billing_transaction_id");
    this.transactions_grid.Relations.Add(relation);
 
}

 

0
Hristo
Telerik team
answered on 09 Jan 2018, 01:56 PM
Hello Joe,

Thank you for writing back.

You can set up the templates and add the relations independently to the data source. However, when you set the data source to the grid or the templates you will need to use a collection returned by your query. As I said before, just check that you are not returning anonymous types to fill the collections used to data bind the control.

I hope this helps. Let me know if you have other questions.

Regards,
Hristo
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
GridView
Asked by
Joe
Top achievements
Rank 2
Answers by
Hristo
Telerik team
Joe
Top achievements
Rank 2
Share this question
or