简体   繁体   中英

Master/Details layout that allows multiple choice in the master table

I have implemented the example from Microsoft which deals with two tables: Master and Detail.

I like how it allows to automatically fill the details grid according to selection in the master grid. But what if I want to select multiple categories ("customers" in the example) in the master grid and get details for all of them?

What would the best best way to implement this behaviour?

In that case, you'd have to do the filtering yourself. Instead of binding the child grid to the relation, you'd bind it to the child table. You'd then handle the appropriate event of the parent grid and then filter the child data based on the selected parent records. Eg

Private Sub parentDataGridView_SelectionChanged(sender As Object, e As EventArgs) Handles parentDataGridView.SelectionChanged
    Dim parentIDs As New List(Of Object)

    For Each selectedRow As DataGridViewRow In parentDataGridView.SelectedRows
        Dim rowView = DirectCast(selectedRow.DataBoundItem, DataRowView)

        parentIDs.Add(rowView("ParentID"))
    Next

    childBindingSource.Filter = If(parentIDs.Any(),
                                   String.Format("ParentID IN ({0})",
                                                 String.Join(", ",
                                                             parentIDs)),
                                   String.Empty)
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM