DataView.RowFilter in a For Loop C#

351 Views Asked by At

Surprisingly, while filtering a DataTable within a For Loop using DataView.RowFilter, Sytem.Data.DataView returns a value for i= 0 (i.e. filter works as expected) whereas it is empty for i>0. Below is an illustration:

for (int i = 0; i < fruitid.Length; i ++)
{
    DataTable dnt = new DataTable();
    dnt = dr.ReadSSData(date, extension); // no issue so far
    DataView dvv = new DataView(dnt);
    dvv.RowFilter = "FRUIT = " + "'" + fruitid[i] + "'" + 
                    " AND BASIS_IND ='A'";
    dnt.Clear(); // i've cleared dnt, reset it even deleted 
    //dvv to ensure the filter gets 
    //re-initialized after each increment, but 
    //it does not improve the situation
}

However while inefficiently filtering the DataTable out of the for loop and by hard-coding the filter n times and also by creating new DataView objects such as:

DataTable dnt = new DataTable();
dnt = dr.ReadSSData(date, extension);

DataView dvv = new DataView(dnt);
dvv.RowFilter = "FRUIT = 'Apple' AND BASIS_IND ='A'";

DataView dvv2 = new DataView(dnt);
dvv2.RowFilter = "FRUIT = 'Orange' AND BASIS_IND ='A'";

the filters (dvv and dvv2) work well.

Thanks in advance for the explanation of this weird phenomenon. Best,

0

There are 0 best solutions below