Search code examples
devexpressmaster-detailxtragridrelation

How to display the master-detail tables into two xtragrid?


I have to tables:users and messages.And create the one-many relations between these tables.And,If I display this two table in single xtragrid, it's no problem,But ,I want to display into two grid,the detail of messages doesn't display. please help me!

the key code is :

1.create the dataset of users and messages:

        DataSet ds = new DataSet();
        ds = SqlHelper.ExecuteDataset(fbh.ConnectionString, CommandType.Text, s);
        DataTable mess = ds.Tables[0];
        mess.TableName = "Messages";
        DataTable user = deptmentUserMessages.Tables["Users"];

        DataTable m = new DataTable("Messages");
        m.Merge(mess);
        deptmentUserMessages.Tables.Add(m);
        deptmentUserMessages.Relations.Add("**UserMessages**", user.Columns["UserName"], m.Columns["SENDER"]);

the deptmentUserMessages is the static dataset

2.in the form.load event,I have to do:

        gcMessage.DataSource = master;
        gcMessageDetail.DataSource = detail;

        master.DataSource = pub.DeptmentUserMessages;
        master.DataMember = "Users";
        detail.DataSource = master;
        detail.DataMember = "**UserMessages**";

the master and detail are BindingSource.


Solution

  • You should handle xtargrid FocusedRowChanged event to populate detail grid.

    Get the primary key field value to fetch values from child table and then set the data source of child grid. Use GetRowCellValue(Int32,String) Method

    private void gridView1_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e) {
    
    if(e.FocusedRowHandle >=0)
    {
    /// Get master table selected row's primary column value
    /// then create a DataView from the detail table 
    // and set datasource of detail grid.
    dvUserMessages = (Filtered row by primary column value);
    
    master.DataSource = dvUserMessages.ToTable();
    
    }
    
    }
    

    Reference:Parent - Child relationship between Two GridViews