
244Microsoft Visual Studio 2010: A Beginner’s Guide
Dim ordList As List(Of Order)
ordList = CType(OrderViewSource.Source, List(Of Order)) Dim ord As Order
ord = ordList.FirstOrDefault()
Dim ctx As New MyShopDataContext
ctx.Orders.InsertOnSubmit(ord)
ctx.SubmitChanges()
MessageBox.Show("Order Saved!")
End Sub
Before the SaveButton_Click event handler ends, it shows a message box to the user with a status message, Order Saved. The MessageBox class has several overloads of the Show method that allows you to specify buttons, icons, and more.
So far, you’ve learned how to create an input form for adding a new record to the database. The next section will build upon this by showing you how to view, modify, and delete records with the DataGrid.
Using the DataGrid
The DataGrid is the best option for working with data that must be shown with multiple rows and columns. This section will show you how to show, update, and delete items with a Grid. First, we’ll display orders.
We’ll build off the data source created in the previous example to show data in a Grid. First, you’ll need to open the Data Source window by selecting Data Open Data Sources. The preceding section specified the CustomerID as a ComboBox. If you’re following along, you’ll want to change CustomerID to a TextBox by clicking on CustomerID for the Order object in the Data Sources window and selecting TextBox. Change the control type of Order from a form to a Grid by selecting the combo box for the Order object in the Data Sources window and selecting the DataGrid option. Open the MainWindow
.xaml file in the Designer and drag and drop Order from the Data Sources window to the Designer. Remember there is a New Order button that you’ll want to move to the bottom of the form. Also, add another button, set its Name property to UpdateButton, and set its Content property to Update. Position the New Order and Update buttons at the bottom of the form. Resize and move controls and form so they look like Figure
Just as with the form view in the preceding section, VS added a CollectionViewSource to the window when adding the Order to the Designer. The following Window_Loaded event handler provides the Order data to display in the Grid: