none
ADO.Net: DataView filter bug??? RRS feed

  • Question

  • // I found some strangeless in behavior of DataView's RowFilterProperty.
    // It works wrong when we use filter which references to properties of parent DataRow.
    // It seems that filter cheks only old values of parent DataRow.

    // Here is the sample (sorry for misformatting, I use Opera...)

    //--> Begin of the bug sample. See the ShowMeTheBug method.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;

    namespace TestData
    {
    static class Program
    {
    private static readonly string relationName = "ParentChild",
    filter = "Parent(" + relationName + ").Name = 'Parent row 1'",
    filter2 = "ParentName = 'Parent row 1'";

    private static DataSet dataSet;
    private static DataTable parentTable, childTable;


    static void Main(string[] args)
    {
    InitDataSet();
    RefillData();
    ShowMeTheBug();
    Console.Write("\r\nDone. Press any key...");
    Console.ReadKey();
    }

    private static void ShowMeTheBug()
    {
    Console.WriteLine(
    @"->Initializing filter...
    Items with parent's row name = 'Parent row 1'
    (3 rows expected):");
    DataView view = new DataView(childTable);
    view.RowFilter = filter;
    ShowCurrentData(view);

    Console.WriteLine(@"
    ->Changing name of the parent row and resetting filter...
    (0 rows expected, but it prints 3 rows!!!):");
    parentTable.Rows[0][1] = "This row does not mathch to filter!!!";
    view.RowFilter = false.ToString();
    view.RowFilter = filter;
    ShowCurrentData(view);

    Console.WriteLine(@"
    ->Accepting changes and resetting filter...
    (0 rows expected):");
    dataSet.AcceptChanges();
    view.RowFilter = false.ToString();
    view.RowFilter = filter;
    ShowCurrentData(view);
    }

    private static void ShowCurrentData(DataView view)
    {
    Console.WriteLine("View data:");
    foreach (DataRowView row in view)
    {
    Console.WriteLine(" Child: '{2}', Parent: '{3}'",
    row[0], row[1], row[2], row[3]);
    }

    /*
    Console.WriteLine("Select data:");
    foreach (DataRow row in childTable.Select(filter))
    {
    Console.WriteLine(" Child: '{2}', Parent: '{3}'",
    row[0], row[1], row[2], row[3]);
    }
    Console.WriteLine("Select by precomputed column value:");
    foreach (DataRow row in childTable.Select(filter2))
    {
    Console.WriteLine(" Child: '{2}', Parent: '{3}'",
    row[0], row[1], row[2], row[3]);
    }
    */
    }

    private static void InitDataSet()
    {
    dataSet = new DataSet();

    parentTable = new DataTable("ParentTable");
    parentTable.Columns.Add("ID", typeof(int));
    parentTable.Columns.Add("Name", typeof(string));

    childTable = new DataTable("ChildTable");
    childTable.Columns.Add("ID", typeof(int));
    childTable.Columns.Add("ParentID", typeof(int));
    childTable.Columns.Add("Name", typeof(string));

    dataSet.Tables.AddRange(new DataTable[] { parentTable, childTable });

    ForeignKeyConstraint fkc = new ForeignKeyConstraint(
    "ParentChild",
    parentTable.Columns["ID"],
    childTable.Columns["ParentID"]);

    fkc.UpdateRule = Rule.Cascade;
    fkc.DeleteRule = Rule.Cascade;
    fkc.AcceptRejectRule = AcceptRejectRule.Cascade;

    childTable.Constraints.Add(fkc);


    dataSet.Relations.Add(
    relationName,
    parentTable.Columns["ID"],
    childTable.Columns["ParentID"]);

    childTable.Columns.Add("ParentName", typeof(string));
    childTable.Columns["ParentName"].Expression = "Parent(" + relationName + ").Name";
    }

    private static void RefillData()
    {
    dataSet.Clear();

    for (int i = 1; i < 3; i++)
    {
    parentTable.Rows.Add(i, "Parent row " + i.ToString());

    for (int j = 1; j < 4; j++)
    {
    childTable.Rows.Add(j + (i - 1) * 3, i,
    "Child " + j.ToString() + " of parent " + i.ToString());
    }
    }

    dataSet.AcceptChanges();
    }
    }
    }
    //End of bug sample.


    Wednesday, November 28, 2007 3:18 AM

All replies