Reproducible bug in Master-Detail DataGrid when RowFilter applied to Master

Reproducible bug in Master-Detail DataGrid when RowFilter applied to Master

Post by jgbaile » Sun, 23 Oct 2005 04:19:27

s a service to the community, I'm posting details about a bug I've
found in the Windows Forms DataGrid control. It only occurs when two
grids are in a master-detail (or parent-child) relationship, and you
apply a RowFilter to the parent grid.

Normally, the detail grid will display only the rows related to the
selected master row. If a filter was applied to the parent grid such
that no rows were displayed, you would expect that the child grid would
also be cleared. However, that is not the case. The child grid will
continue to display whatever rows it was previously displaying.

The code at the end of this message is a simple app which demonstrates
the problem. It has two grids, the master on the left and the detail on
the right. It also has a text box below. Anything typed into the
textbox will apply a filter to the master grid so that only rows in
which the first column matches the text box are displayed.

When you initially load the application, the first row in the master
grid has a "c" in Column1, and the detail row displays a "5" in
Column2. If you click in the textbox and type "c", the master grid
filter all rows except the one row contianing "c" as expected. The
detail grid continues to show the same row, also as expected. Of
course, the "add new" row is displayed in both grids, too, but that is
not relevant.

However, if you now type "z" so the textbox contain "cz", the master
grid will display no rows (except the "add new" row, of course) while
the detail grid remains unchanged! It still displays the child row of
the now hidden "c" row from the master.

Clearly, this is not expected behavior, and the bug is demonstrated.

I've included a work around in the code attached. I had to derive a new
DataGrid in which I detect if the grid is in a master-detail
relationship, and if so special behavior is applied. Whenever the
parent displays zero rows, the child grid sets a filter on itself such
that no rows will display (i.e. RowFilter = "1 = 0"). You can see this
code in action by uncommenting the "#define WORKAROUND" directive at
the top of the sample blow and running it again. Follow the steps
above, and you will see the child grid behave correctly.

I hope this code helps somebody, as this issue was a huge pain in the
a** to track down and fix. I *especially* hope Microsoft see's this one
and fixes it!


p.s.: Don't view this code in the designer, as it will wipe out the
#if/#else/#endif directives in the InitializeComponent() method and you
will not see the work around take effect.

p.p.s.: I am running Windows XP Pro and using the .NET 1.1 Framework.

---- code follows ----

//#define WORKAROUND

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;

namespace GridBug
public class Form1 : System.Windows.Forms.Form
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.DataGrid dataGrid2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.ComponentModel.Container components = null;

public Form1()

protected override void Dispose( bool disposing )
if( disposing )
if (components != null)
base.Dispose( disposing );

private void

1. Master-detail: why doesn't detail DS automatically track row of master (i.e. :param not updated)?

2. Master/Detail DVWP: New Master search still shows old Detail

Hello -

I created a pretty simple master/detail setup with two data views going
against a SQL database. Master DVWP uses a search box to filter the
results, and then clicking each item in the Master filtered list shows
the Detail below.

However, here's my problem:

1) Load up web page, empty search (no Master list since there is no
filter, no Detail shown).
2) Enter search string and do postpack, Master lists A and B. Detail
shows A, as it defaults to show the first row.
3) Click "B". Master shows B as selected, Detail shows B.
4) Enter new Master search, do postback, Master lists C and D. Detail
still shows B.

#4 should have Detail shows C, being the first row in the list. But
somehow that doesn't get passed to the Detail after changing the Master

If I skip step #3, and never choose an item to display in the detail
and always just have it default to the first row, then #4 works as
expected. It's only when I manually trigger the web part connection.

So my question is how to clear that parameter so that when I re-filter
the Master it goes back to passing the first row value to the Detail?
Is there another parameter or call that needs to happen during the
Master filtering?

Thanks for any help. This one has me stumped.


3. Master Detail Report With Master Chart and Detail Table

4. CachedUpdate in a master/detail query from another master/detail query

5. Master Detail commits the master on detail exception

6. Master-detail: I see all the detail records if I append a detail record

7. Master-Detail not working when Master.Eof is True

8. Master-Detail Datagrid -checkbox (once tick the checkbox, all the child checkbox is ticked)

9. How do I use a combobox for master-detail binding with a datagrid?

10. Show Master-Detail Data in separate datagrid

11. DataGrid: Master-Detail

12. Master-Detail DataGrid with two tables created on the fly

13. Master-Detail datagrid[allownew = false]

14. Master-Detail Datagrid sum up problem

15. Accordion/Datagrid/Master-Detail Question