3.9. Using a DataView with a Strongly Typed DataSet
Problem
When using a DataView to find rows in a typed DataSet, you want to convert the rows you find to typed DataRow objects with all the properties and methods defined in your typed DataSet.
Solution
Cast the DataRow object returned by the Row property of a DataView to a typed DataRow.
The solution creates the strongly typed DataSet AdventureWorks and fills it from the Sales.SalesOrderHeader and Sales.SalesOrderDetail tables in AdventureWorks. A DataView is created of the Sales.SalesOrderHeader records where the TotalDue amount is greater than 175,000. The solution iterates over the collection of rows in the DataView, casting each to the strongly types AdventureWorks.SalesOrderHeaderRow row and uses the properties of the strongly typed row to output column values to the console.
The C# code in Program.cs in the project DataViewStronglyTypedDataSet is shown in Example 3-12.
Example 3-12. File: Program.cs for DataViewStronglyTypedDataSet solution
using System; using System.Data; using System.Data.SqlClient; namespace DataViewStronglyTypedDataSet { class Program { static void Main(string[] args) { string sqlConnectString = "Data Source=(local);" + "Integrated security=SSPI;Initial Catalog=AdventureWorks;"; string sqlSelect = "SELECT * FROM Sales.SalesOrderHeader;" + "SELECT * FROM Sales.SalesOrderDetail;"; AdventureWorks awds = new AdventureWorks( ); // Create a DataAdapter to fill the DataSet SqlDataAdapter da = new SqlDataAdapter(sqlSelect, ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access