Lab Creating Data Table Objects

In this lab you will create DataTable objects.

► Exercise 1: Creating a DataTable

The following example describes how to create a DataTable as well as how to create a new row of data and add it to the table. For demonstration purposes, the DataTable is displayed in a DataGridView.

1. Create a Windows application and name it CreatingDataTables.

2. Add a DataGridView to Forml and name it TableGrid.

3. Add a button to the form and set the following properties:

□ Name = CreateTableButton

4. Add a second button to the form and set the following properties:

5. Double-click each button to create the button click event handlers.

6. Add the following code to the form (global to the form, outside any methods):

' Instantiate a new table (global to the form).

Private CustomersTable As New DataTab1e("Customers") // C#

// Instantiate a new table (global to the form).

private DataTable CustomersTable = new DataTab1e("Customers");

7. Add the following code to the CreateTableButton_Click event handler:

' Set the DataGridView to display the table TableGrid.DataSource = CustomersTable

' Define the schema of the table by adding DataColumn objects to ' the table's Columns collection.

CustomersTab1e.Co1umns.Add("CustomerID", Type.GetType("System.String")) CustomersTab1e.Co1umns.Add("CompanyName", Type.GetType("System.String")) CustomersTab1e.Co1umns.Add("ContactName", Type.GetType("System.String")) CustomersTable.Columns.AddC'ContactTitle", Type.GetType("System.String")) CustomersTab1e.Co1umns.Add("Address", Type.GetType("System.String")) CustomersTab1e.Co1umns.Add("City", Type.GetType("System.String")) CustomersTab1e.Co1umns.Add("Region", Type.GetType("System.String")) CustomersTable.Col umns.Add("Posta1Code", Type.GetType("System.String")) CustomersTab1e.Co1umns.Add("Country", Type.GetType("System.String")) CustomersTab1e.Co1umns.Add("Phone", Type.GetType("System.String")) CustomersTab1e.Co1umns.Add("Fax", Type.GetType("System.String"))

' Set the CustomerlD column As the primary key Dim KeyColumns(l) As DataColumn

KeyColumns(O) = CustomersTab1e.Co1umns("CustomerID") CustomersTable.PrimaryKey = KeyColumns

' Set the CustomerlD and CompanyName columns ' to disallow Null values.

CustomersTab1e.Co1umns("CustomerID").A11owDBNu11 CustomersTab1e.Co1umns("CompanyName").AllowDBNull

// Set the DataGridView to display the table TableGrid.DataSource = CustomersTable;

// Define the schema of the table // by adding DataColumn objects to // the table's Columns collection.

CustomersTab1e.Co1umns.Add("CustomerID", Type.GetType("System.String")); CustomersTab1e.Co1umns.Add("CompanyName", Type.GetType("System.String")); CustomersTab1e.Co1umns.Add("ContactName", Type.GetType("System.String")); CustomersTab1e.Co1umns.Add("ContactTit1e", Type.GetType("System.String")); CustomersTab1e.Co1umns.Add("Address", Type.GetType("System.String")); CustomersTab1e.Co1umns.Add("City", Type.GetType("System.String")); CustomersTab1e.Co1umns.Add("Region", Type.GetType("System.String")); CustomersTable.Col umns.Add("Posta1Code", Type.GetType("System.String")); CustomersTab1e.Co1umns.Add("Country", Type.GetType("System.String")); CustomersTab1e.Co1umns.Add("Phone", Type.GetType("System.String"));

CustomersTab1e.Co1umns.Add("Fax", Type.GetType("System.String"));

// Set the CustomerID column as the primary key

DataCo1umn[] KeyColumns = new DataCo1umn[1];

KeyCo1umns[0] = CustomersTab1e.Co1umns["CustomerID"];

CustomersTable.PrimaryKey = KeyColumns;

// Set the CustomerlD and CompanyName columns

// to disallow Null values.

CustomersTab1e.Co1umns["CustomerID"].A11owDBNu11 = false;

CustomersTab1e.Co1umns["CompanyName"].A11owDBNu11 = false;

8. Add the following code to the AddRowButton_Click event handler:

' Create a record (DataRow) to add to the table.

Dim CustRow As DataRow = CustomersTable.NewRow

With CustRow

.Item("CustomerID") = "ALFKI" .Item("CompanyName") = "Alfreds Futterkiste" .Item("ContactName") = "Maria Anders" .Item("ContactTit1e") = "Sales Representative" .Item("Address") = "Obere Str. 57" .Item("City") = "Berlin" .Item("Region") = Nothing .Item("Posta1Code") = "12209" .Item("Country") = "Germany" .Item("Phone") = "030-0074321" .Item("Fax") = "030-0076545"

End With

' Add the record to the table.

CustomersTable.Rows.Add(CustRow)

// Create a record (DataRow) to add to the table.

DataRow CustRow = CustomersTab1e.NewRow();

Object[] CustRecord = {"ALFKI", "Alfreds Futterkiste", "Maria Anders", "Sales Representative", "Obere Str. 57", "Berlin", null, "12209", "Germany", "030-0074321","030-0076545"};

CustRow.ItemArray = CustRecord;

// Add the record to the table.

CustomersTable.Rows.Add(CustRow);

9. Run the application and click the Create Table button.

The grid displays an empty data table with the columns as shown in Figure 7-9.

Figure 7-9 Form after clicking the Create Table button

10. Click the Add Row button, and a new record is added to the table, as shown in Figure 7-10.

Figure 7-10 Form after clicking the Add Row button

11. Click the Add Row button again, and note the exception that occurs.

Was this article helpful?

0 0

Post a comment