Creating a DataTable by filtering another DataTable

Posted by Jeff Dege on Stack Overflow See other posts from Stack Overflow or by Jeff Dege
Published on 2010-12-23T19:42:03Z Indexed on 2010/12/23 20:54 UTC
Read the original article Hit count: 386

Filed under:
|

I'm working on a system that currently has a fairly complicated function that returns a DataTable, which it then binds to a GUI control on a ASP.NET WebForm.

My problem is that I need to filter the data returned - some of the data that is being returned should not be displayed to the user.

I'm aware of DataTable.select(), but that's not really what I need. First, it returns an array of DataRows, and I need a DataTable, so I can databind it to the GUI control. But more importantly, the filtering I need to do isn't something that can be easily put into a simple expression. I have an array of the elements which I do not want displayed, and I need to compare each element from the DataTable against that array.

What I could do, of course, is to create a new DataTable, reading everything out of the original, adding to the new what is appropriate, then databinding the new to the GUI control. But that just seems wrong, somehow. In this case, the number of elements in the original DataTable aren't likely to be enough that copying them all in memory is going to cause too much trouble, but I'm wondering if there is another way.

Does the .NET DataTable have functionality that would allow me to filter via a callback function?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about datatable