Free mag vol1 | Page 941

CHAPTER 22  ADO.NET PART II: THE DISCONNECTED LAYER To see this in action, update the current UI with an additional DataGridView type named dataGridYugosView and a descriptive Label. Next, define a member variable named yugosOnlyView of type DataView. public partial class MainForm : Form { // View of the DataTable. DataView yugosOnlyView; ... } Now create a new helper function named CreateDataView() and call this method within the form’s default constructor immediately after the DataTable has been fully constructed, as shown here: public MainForm() { ... // Make a data table. CreateDataTable(); } // Make a view. CreateDataView(); Here is the implementation of this new helper function. Notice that the constructor of each DataView has been passed the DataTable that you will use to build the custom set of data rows. private void CreateDataView() { // Set the table that is used to construct this view. yugosOnlyView = new DataView(inventoryTable); // Now configure the views using a filter. yugosOnlyView.RowFilter = "Make = 'Yugo'"; // Bind to the new grid. dataGridYugosView.DataSource = yugosOnlyView; } As you can see, the DataView class supports a property named RowFilter, which contains the string representing the filtering criteria used to extract matching rows. After you establish your view, set the grid’s DataSource property accordingly. Figure 22-10 shows the completed Windows Forms data-binding application in action. 887