KB Article A1600
Visible to All Users

How to display a self-referenced data table via master and detail grid views

Description:
I'm trying to bind the XtraGrid to a table that has a recursive relationship, eg. Employee-ReportsTo. I would like the grid to behave in a similar manner to your XtraTreeList - to build a tree according to the relations in the table. Can this be done?

Answer:
The XtraTreeList is best used for displaying self-referenced tables. However, you can also use the XtraGrid for this task.
You will need to create a relation for your data table (use the DataSet Schema editor) and bind the grid to the table. It will automatically allow you to expand master rows to see child records.
In this scenario, the main grid view displays all the table rows. You will need to filter the grid's data source to show only "root" records. Here is the code to bind the grid to the filtered data:

C#
DataView dv = new DataView(dataset11.Products); dv.RowFilter = "([ParentID] Is NULL)"; gridControl1.DataSource = dv;
Visual Basic
Dim dv As DataView = New DataView(dataset11.Products) dv.RowFilter = "([ParentID] Is NULL)" gridControl1.DataSource = dv

You may wish to set up detail views to have the same layout as the main grid view. Please handle the MasterRowGetLevelDefaultView event of the main grid view to implement this:

C#
private void gridView1_MasterRowGetLevelDefaultView(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetLevelDefaultViewEventArgs e) { GridView view = new GridView(gridControl1); view.Assign(gridControl1.MainView, true); view.OptionsView.ShowGroupPanel = false; e.DefaultView = view; }
Visual Basic
Imports DevExpress.XtraGrid.Views.Grid Private Sub gridView1_MasterRowGetLevelDefaultView(ByVal sender As Object, ByVal e As MasterRowGetLevelDefaultViewEventArgs) Handles gridView1.MasterRowGetLevelDefaultView Dim view As GridView = New GridView(gridControl1) view.Assign(gridControl1.MainView, True) view.OptionsView.ShowGroupPanel = False e.DefaultView = view End Sub

See also:
Master-Detail: DataTable Use
Filtering Overview

Show previous comments (1)
Andrew Ser (DevExpress Support) 11 years ago

    Hello,
    Let's discuss the case with several relations in the How to display a self-referenced data table via master and detail grid views with several relations thread.

      Hi
      How to remove columns header from sub levels and making sure sub levels columns are aligned with the grid header like the XtraTreeView.

      I cannot use XTreeView since i have hundreds off hours of coding around a gridview.

      DevExpress Support Team 8 years ago

        I see that you've asked a similar question in the GridView - How to hide the detail view's tab header, group panel and column headers thread. I've posted my answer there.
        Please don't duplicate your questions since it increases the response time and complicates processing of your tickets.

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.