CubanGT
CubanGT

Reputation: 351

Why is my filter not working? d.DefaultView.RowFilter = cls;

I merely want to apply a row filter to my datatable and its not working, no errors, just get back the same datatable.

Ive tried creating a dataview from the datatable in order to apply, but that also doesnt return the filtered list. Im lost as to why this code below doesnt work..

DataTable d = processFileData(concatFile);
string cls = String.Format("Column6 NOT IN ({0})", String.Join(",", returnClass()));
d.DefaultView.RowFilter = cls;

The cls variable above is a list of int values im trying to use which looks like this:

Column6 NOT IN (75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,281,303,405,406,493,599,601,606,610,626,630,639,640,647,648,651,662,664,671,672,680,689,697,701,703,706,709,712,717,718,724,735,740,741,743,744,767,768,769,770,775,780,791,799,800,801,802,803,805,806,807,808,810,811,812,815,816,817,818,820,836,837,873,874,875,879,881,895,896,897,902,903,904)

I was hoping to have a new set of records with only the ones that didnt have the condition in my filter. Original DataTable = 34,945 records, if i manually apply my filter in excel on the file, the expected results should be DataTable = 29,240 records.

Here is example of what im trying to accomplish with the filter:

Original DataTable:

Column1, Column2, Column4, Column5, Column6

Hello, Today, Food, Fun, 75

Hello, Today, Food, Fun, 75

Hello, Today, Food, Fun, 79

Hello, Today, Food, Fun, 79

Hello, Today, Food, Fun, 79

Hello, Today, Food, Fun, 100

Hello, Today, Food, Fun, 101

Hello, Today, Food, Fun, 700

Hello, Today, Food, Fun, 750

Hello, Today, Food, Fun, 749

Hello, Today, Food, Fun, 755

Hello, Today, Food, Fun, 799

Hello, Today, Food, Fun, 799

Hello, Today, Food, Fun, 804

Expected DataTable After Filtering based on Column6 values:

Column1, Column2, Column4, Column5, Column6

Hello, Today, Food, Fun, 100

Hello, Today, Food, Fun, 101

Hello, Today, Food, Fun, 700

Hello, Today, Food, Fun, 750

Hello, Today, Food, Fun, 749

Hello, Today, Food, Fun, 755

Hello, Today, Food, Fun, 804

Upvotes: 1

Views: 562

Answers (2)

SAR
SAR

Reputation: 310

 // list of values to be filtered
            List<int> filter = new List<int>()
            {
               75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,281,303,405,406,493,599,601,606,610,626,630,639,640,647,648,651,662,664,671,672,680,689,697,701,703,706,709,712,717,718,724,735,740,741,743,744,767,768,769,770,775,780,791,799,800,801,802,803,805,806,807,808,810,811,812,815,816,817,818,820,836,837,873,874,875,879,881,895,896,897,902,903,904
            }; 

// LINQ statement to do filtering
  IEnumerable<DataRow> unmatchingRows = from DataRow row in dt.Rows
                                      where !filter.Contains((int)row[0])
                                      select row;
  // add to a new datatable
  DataTable output = dt.Clone();
  foreach (DataRow item in unmatchingRows)
  {
      output.ImportRow(item);
  }

Upvotes: 0

SAR
SAR

Reputation: 310

You can just use linq for this instead of DataView

List<int> filter = new List<int>()
            {
               75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,281,303,405,406,493,599,601,606,610,626,630,639,640,647,648,651,662,664,671,672,680,689,697,701,703,706,709,712,717,718,724,735,740,741,743,744,767,768,769,770,775,780,791,799,800,801,802,803,805,806,807,808,810,811,812,815,816,817,818,820,836,837,873,874,875,879,881,895,896,897,902,903,904
            }; 


DataTable output = dt.AsEnumerable().Where((row,index) => !filter.Contains(index)).CopyToDataTable();

Hope this answers your question

Upvotes: 1

Related Questions