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
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 ----
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;
protected override void Dispose( bool disposing )
if( disposing )
if (components != null)
base.Dispose( disposing );