Strong Typed Filtered DataTable
I'm sure most of you have already been able to create a strongly-typed DataSet and have benefited much from design-time validation. One problem arise when you need to query the DataTable to retrieve DataRows based on certain criteria. Using DataTable (or inherited strong-typed DataTable) class, there are two methods you can use to filter the rows, .Select and .Find. The problem is, both of them will return the basic DataRow object, which means they lost any information about strong typing.
Using Visual Studio 2005, you can use TableDataAdapter to add a query, and keep the strong-typed DataRow. Here's the steps:
- I assumed that you already have a typed DataSet, to start, open the DataSet Designer.
- On [TableName] group, right click it, and select Add > Query.
- You can use any method to retrieve the table. On the Choose Method to Generate page, choose either Fill a DataTable or Return a DataTable. Personally, I prefer the latter method.
- You can then use the methods to retrieve a filtered-typed-data table.