How to connection two tables(master-detail) in devexpress treelist?

1.7k views Asked by At

I have two tables: Department and User. The department is a tree-style structure with the field PDeptID and DeptID (its key), so I put it into the XtraTreeList component.

The User table linked to Department with DeptAndUser table, and there are DeptID,UserId fields within it.

I want to setup the TreeList with Department and when I click the department ,the right grid control will display all the users in this department.

How do I accomplish this idea?

1

There are 1 answers

0
Jay On BEST ANSWER

I generally have the id field as a TreeListColumn that I can choose not to display in cases where its not useful to the user. That way I still have easy access to my objects id.

There are several possibilities based on just the information provided to access the actual users that match the departmentId. For example, if you have all the users in a list you could use linq to get just the ones that match the department selected in the treeList and then use that for a datasource.

For example:

VB.NET:

Private Sub TreeList1_FocusedNodeChanged(sender As Object, e As DevExpress.XtraTreeList.FocusedNodeChangedEventArgs) Handles TreeList1.FocusedNodeChanged
    If e.Node IsNot Nothing Then
        Dim id As Integer = CInt(e.Node.GetValue("Id"))
        Dim ds As List(Of UserInfo) = (From user As UserInfo In UserList Where user.DeptId = id Select user).ToList
        GridControl1.DataSource = ds
        GridControl1.RefreshDataSource()
    End If
End Sub

C#:

private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            if (e.Node != null) {
                int id = Convert.ToInt32(e.Node.GetValue("Id"));
                List<UserInfo> ds = (from user in UserList where user.DeptId == id select user).ToList;
                GridControl1.DataSource = ds;
                GridControl1.RefreshDataSource();
           }
        }

If your department object actually has a list of its users as a property, you could just access that list directly from that instance in your treeList datasource and use it as the datasource for the grid. No matter your methodology for getting/storing the user data you will likely need to handle the focusedNodeChanged Event.