15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

XMl schemas: Generating Code with XsD ❘ 847<br />

The ProductDataTable class, derived from DataTable <strong>and</strong> implementing the IEnumerable interface,<br />

defines a private DataColumn instance for each of the columns within the table. These are initialized again<br />

from the constructor by calling the private InitClass() member. Each column is used by the DataRow class<br />

(which is described shortly):<br />

[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]<br />

[global::System.CodeDom.Compiler.GeneratedCodeAttribute(<br />

"System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]<br />

[global::System.ComponentModel.Browsable(false)]<br />

public int Count {<br />

get {<br />

return this.Rows.Count;<br />

}<br />

}<br />

// Other row accessors removed for clarity — there is one for each column<br />

code download product.cs<br />

Adding rows to the table is taken care of by the two overloaded (<strong>and</strong> significantly different)<br />

AddProductRow() methods. The first takes an already constructed DataRow <strong>and</strong> returns a void. The second<br />

takes a set of values, one for each of the columns in the DataTable, constructs a new row, sets the values<br />

within this new row, adds the row to the DataTable object, <strong>and</strong> returns the row to the caller. Such widely<br />

different functions shouldn’t really have the same name!<br />

[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]<br />

[global::System.CodeDom.Compiler.GeneratedCodeAttribute(<br />

"System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]<br />

public ProductRow AddProductRow(string ProductName, int SupplierID,<br />

int CategoryID, string QuantityPerUnit, decimal UnitPrice,<br />

short UnitsInStock, short UnitsOnOrder, short ReorderLevel,<br />

bool Discontinued) {<br />

ProductRow rowProductRow = ((ProductRow)(this.NewRow()));<br />

object[] columnValuesArray = new object[] {<br />

null,<br />

ProductName,<br />

SupplierID,<br />

CategoryID,<br />

QuantityPerUnit,<br />

UnitPrice,<br />

UnitsInStock,<br />

UnitsOnOrder,<br />

ReorderLevel,<br />

Discontinued};<br />

rowProductRow.ItemArray = columnValuesArray;<br />

this.Rows.Add(rowProductRow);<br />

return rowProductRow;<br />

}<br />

code download product.cs<br />

Just like the InitClass() member in the DataSet-derived class, which added the table into the DataSet<br />

class, the InitClass() member in ProductDataTable adds columns to the DataTable class. Each<br />

column’s properties are set as appropriate, <strong>and</strong> the column is then appended to the columns collection:<br />

[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]<br />

[global::System.CodeDom.Compiler.GeneratedCodeAttribute(<br />

"System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]<br />

private void InitClass() {<br />

this.columnProductID = new<br />

global::System.Data.DataColumn("ProductID",<br />

typeof(int), null, global::System.Data.MappingType.Element);<br />

base.Columns.Add(this.columnProductID);<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!