Free mag vol1 | Page 932

CHAPTER 22  ADO.NET PART II: THE DISCONNECTED LAYER  Note The next example assumes you have some experience using Windows Forms to build graphical user interfaces. If this is not the case, you might want to open the solution and follow along or return to this section after you have read Appendix A. Your next task is to build a Windows Forms application that will display the contents of a DataTable object within its user interface. Along the way, you will also examine how to filter and change table data. You will also learn about the role of the DataView object. Begin by creating a brand-new Windows Forms project workspace named WindowsFormsDataBinding. Rename your initial Form1.cs file to the more fitting MainForm.cs using the Solution Explorer. Next, use the Visual Studio Toolbox to drag a DataGridView control located in the Data tab (renamed to carInventoryGridView using the (Name) property of the Properties window) onto the designer surface. You might notice that when you first add the DataGridView on the designer, you activate a context menu that allows you to connect to a physical data source. For the time being, ignore this aspect of the designer because you will be binding your DataTable object programmatically. Finally, add a descriptive Label to your designer for information purposes. Figure 22-5 shows one possible lookand-feel. Figure 22-5. The initial GUI of your Windows Forms application Hydrating a DataTable from a Generic List Similar to the previous SimpleDataSet example, the WindowsFormsDataBinding application will construct a DataTable that contains a set of DataColumns representing various columns and rows of data. This time, however, you will fill the rows using a generic List member variable. First, insert a new C# class into your project (named Car), which you define as follows: 878