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

parent /child relationship with multiple key columns

2 Answers 311 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Steven
Top achievements
Rank 1
Steven asked on 26 Mar 2015, 04:29 PM
cant seems to get gridview to pay attention to multiple key fields in the relationship hierarchy
code is like this

GridViewRelation relation = new GridViewRelation(this.radGridStatement.MasterTemplate);
            relation.ChildTemplate = template;
            relation.ParentColumnNames.Add("DealerID");
            relation.ParentColumnNames.Add("Month");
            relation.ParentColumnNames.Add("Year");
            relation.ChildColumnNames.Add("DealerID");
            relation.ChildColumnNames.Add("Month");
            relation.ChildColumnNames.Add("Year");
            this.radGridStatement.Relations.Add(relation);
relationship requires three key columns
gridview only uses first one (dealerid)

2 Answers, 1 is accepted

Sort by
0
Mahmoud
Top achievements
Rank 1
answered on 29 Mar 2015, 09:23 AM
hi Steven 

If you mean self-references in radgridview

You can use this code


        private static DataTable CreateDataSource()
        {
            DataTable dataSource = new DataTable("fileSystem");
            dataSource.Columns.Add("ID", typeof(int));
            dataSource.Columns.Add("ParentID", typeof(int));
            dataSource.Columns.Add("Name", typeof(string));
            dataSource.Columns.Add("Date", typeof(DateTime));
            dataSource.Columns.Add("Type", typeof(string));
            dataSource.Columns.Add("Size", typeof(int));

            dataSource.Rows.Add(1, null, "Program Files", DateTime.Now.AddDays(-100), "Folder", 5120);
            dataSource.Rows.Add(2, 1, "Visual Studio 2010", DateTime.Now.AddDays(-100), "Folder", 3220);
            dataSource.Rows.Add(3, 2, "bin", DateTime.Now.AddDays(-100), "Folder", 3220);
            dataSource.Rows.Add(4, 2, "READEME.txt", DateTime.Now.AddDays(-100), "Text Document", 3);

            dataSource.Rows.Add(5, 1, "Telerik RadControls", DateTime.Now.AddDays(-10), "Folder", 3120);
            dataSource.Rows.Add(6, 5, "Telerik UI for Winforms", DateTime.Now.AddDays(-10), "Folder", 101);
            dataSource.Rows.Add(7, 5, "Telerik UI for Silverlight", DateTime.Now.AddDays(-10), "Folder", 123);
            dataSource.Rows.Add(8, 5, "Telerik UI for WPF", DateTime.Now.AddDays(-10), "Folder", 221);
            dataSource.Rows.Add(9, 5, "Telerik UI for ASP.NET AJAX", DateTime.Now.AddDays(-10), "Folder", 121);

            dataSource.Rows.Add(10, 1, "Microsoft Office 2010", DateTime.Now.AddDays(-120), "Folder", 1230);
            dataSource.Rows.Add(11, 10, "Microsoft Word 2010", DateTime.Now.AddDays(-120), "Folder", 1230);
            dataSource.Rows.Add(12, 10, "Microsoft Excel 2010", DateTime.Now.AddDays(-120), "Folder", 1230);
            dataSource.Rows.Add(13, 10, "Microsoft Powerpoint 2010", DateTime.Now.AddDays(-120), "Folder", 1230);

            dataSource.Rows.Add(14, 1, "Debug Diagnostic Tools v1.0", DateTime.Now.AddDays(-400), "Folder", 2120);
            dataSource.Rows.Add(15, 1, "Designer's 3D Tools", DateTime.Now.AddDays(-500), "Folder", 1120);
            dataSource.Rows.Add(16, 1, "Communication", DateTime.Now.AddDays(-700), "Folder", 120);

            dataSource.Rows.Add(17, null, "My Documents", DateTime.Now.AddDays(-200), "Folder", 1024);
            dataSource.Rows.Add(18, 17, "Salaries.xlsx", DateTime.Now.AddDays(-200), "Excel File", 1);
            dataSource.Rows.Add(19, 17, "RecessionAnalysis.xlsx", DateTime.Now.AddDays(-200), "Excel File", 1);

        

            dataSource.Rows.Add(33, null, "Boot.ini", DateTime.Now.AddDays(-10), "INI File", 0);

            return dataSource;
        }

        protected override void OnLoad(EventArgs e)
        {
 
            this.radGridView1.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            this.radGridView1.DataSource = CreateDataSource();
            this.radGridView1.Relations.AddSelfReference(this.radGridView1.MasterTemplate, "ID", "ParentID");

            this.radGridView1.Columns["ID"].IsVisible = false;
            this.radGridView1.Columns["ParentID"].IsVisible = false;
            this.radGridView1.Columns["Size"].FormatString = "{0} MB";

        }

 and You can find this code in Demo Application - Telerik UI for WinForms , In case installed on your computer


0
Hristo
Telerik team
answered on 31 Mar 2015, 12:16 PM
Hello guys,

Mahmoud, thank you for sharing this example with the community. I must add that whether creating a self referencing RadGridView element or a normal hierarchy it is essential that the ParentColumnNames and ChildColumnNames collections be filled with the names of the fields of the corresponding data sources.

I believe that the following articles will be of help: Binding to Hierarchical Data ProgrammaticallySelf-Referencing Hierarchy. I prepared a demo project which programmatically creates a hierarchy.

I hope this information was useful. Should you have further questions please do not hesitate to write back.

Regards,
Hristo Merdjanov
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
Tags
GridView
Asked by
Steven
Top achievements
Rank 1
Answers by
Mahmoud
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or