Free mag vol1 | Page 919

CHAPTER 22  ADO.NET PART II: THE DISCONNECTED LAYER Expression This property gets or sets the expression used to filter rows, calculate a column’s value, or create an aggregate column. Ordinal This property gets the numerical position of the column in the Columns collection maintained by the DataTable. ReadOnly This property determines whether this column is read-only, once a row has been added to the table. The default is false. Table This property gets the DataTable that contains this DataColumn. Unique This property gets or sets a value indicating whether the values in each row of the column must be unique or if repeating values are permissible. If you assign a column a primary key constraint, then you must set the Unique property to true. Building a DataColumn To continue with the SimpleDataSet project (and illustrate the use of the DataColumn), assume you would like to model the columns of the Inventory table. Given that the CarID column will be the table’s primary key, you will configure this DataColumn object as read-only, unique, and non-null (using the ReadOnly, Unique, and AllowDBNull properties). Next, update the Program class with a new method named FillDataSet(), which you use to build four DataColumn objects. Note this method takes a DataSet object as its only parameter. static void FillDataSet(DataSet ds) { // Create data columns that map to the // "real" columns in the Inventory table // of the AutoLot database. DataColumn carIDColumn = new DataColumn("CarID", typeof(int)); carIDColumn.Caption = "Car ID"; carIDColumn.ReadOnly = true; carIDColumn.AllowDBNull = false; carIDColumn.Unique = true; } DataColumn carMakeColumn = new DataColumn("Make", typeof(string)); DataColumn carColorColumn = new DataColumn("Color", typeof(string)); DataColumn carPetNameColumn = new DataColumn("PetName", typeof(string)); carPetNameColumn.Caption = "Pet Name"; Notice that when you configure the carIDColumn object, you assign a value to the Caption property. This property is helpful because it allows you to define a string value for display purposes, which can be distinct from the literal database table column name (column names in a literal database table are typically better suited for programming purposes [e.g., au_fname] than display purposes [e.g., Author First Name]). Here, you set the caption for the PetName column for the same reason, because Pet Name looks nicer than PetName to the end user. 865