Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft CSharp Programming For The Absolute Beginner (2002) [eng].pdf
Скачиваний:
39
Добавлен:
16.08.2013
Размер:
15.71 Mб
Скачать

Figure 11.45: The XML quiz file from the last chapter can be viewed as a data set.

Creating the SpyMaster Database

The SpyMaster database puts together all the database concepts you have seen so far and adds a few more, to build a complete agent−handling solution. The SpyMaster program reassembles all the data taken apart in the data normalization process and puts it in a form that makes sense to the user.

Building the Main Form

Like many projects, the SpyMaster program features a main screen as a control panel. Each button on the main screen calls another editor. Because the code for the main form is so much like code you’ve seen before, I won’t reproduce it here. You can see it on the CD−ROM if you wish.

Trick When I was debugging a particular form, I changed my main form’s Main() method so it immediately called up the form I was working on. For example, when I was working on the Agent Editor form (which I worked on for quite some time, incidentally) I had the following code in the Main() method of the main form:

Application.Run(new AgentEdit());

This caused the Agent Edit form to pop up immediately without the menu screen ever appearing. Of course, when you’re done debugging, you’ll need to change the Main() method back so it starts itself up instead.

361

Some of the editing forms are much simpler than the others, so I will show them to you from simple to complex, rather than in the order the buttons appear on the main form.

Editing the Assignments

It is reasonably easy to edit any data table in your original database. The EditAssignments form enables the user to modify the Assignments table and add new assignments. The visual layout of EditAssignments is shown in Figure 11.46.

Figure 11.46: All that’s needed is a data grid, a button, and some data connection controls.

I drug the Assignment table to the form and renamed my connection and adapter objects. I then created a data set based on the Adapter, renamed it, and bound the data grid to the data set.

Trap If you want the editor to be able to update information, your data set must point to a table, not a view. Views cannot be edited because they often come from more than one table.

The only initialization necessary is to fill the data set from the adapter.

private void EditAssignment_Load(object sender, System.EventArgs e) {

AssignAdapter.Fill(spyDS);

}

The user can modify elements in the data grid. When the user presses the Update button, the code to update the database is simplicity:

private void btnUpdate_Click(object sender, System.EventArgs e) {

AssignAdapter.Update(spyDS);

}

362

Remember that the data set is just a copy of the data. To make changes to the original database, you must use the Update() method of the appropriate data adapter. This method requires you to send a data set with the new information. Because the data set is bound to the grid, any changes made in the data grid are reflected on the data set.

Hint This is one of the advantages of .NET’s data model: The user can experiment with changes to the data set without disturbing the original database. No changes are made to the original database until the adapter’s Update() method is called. This gives you a chance to check the code and make sure the changes work before changing the original database.

Editing the Specialties

Editing the Specialties table is just like editing the Assignments table. The EditSpecialties form looks very similar to EditAssignments, as you can see from Figure 11.47.

Figure 11.47: The EditSpecialties form has a data grid, a button, and data connection objects much like EditAssignments.

The form’s load method fills the data table just like in EditAssignments.

private void button1_Click(object sender, System.EventArgs e) {

specAdapter.Update(spyDS);

}

This form is directly connected to the Specialties table. Once again, the Update button simply calls the data adapter’s Update() method.

private void button1_Click(object sender, System.EventArgs e) {

specAdapter.Update(spyDS);

}

363

Viewing the Agents

It’s a little trickier to edit the agent information, because this information comes from a number of tables. However, viewing the agent information is not too tricky if you start with a data view.

Building the Visual Layout of ViewAgents

The ViewAgents form is based on a couple of data views. It is featured in Figure 11.48.

Figure 11.48: The top combo box is used to choose an agent. The current agent’s data is shown on the various labels, and the list box displays all the skills associated with the current agent.

Notice that I have made multiple data adapters. The AAAdapter connects to a view called Agents_Assignments. The data set called theAAdataSet is based on

AAAdapter. I also created another adapter called ASAdapter, which connects to the Agent_Specialty view. The ASdataSet connects to the ASAdapter. Note that only one Connection object is necessary, because both adapters can use the same connection. (It’s also possible to use only one data set, as I’ll illustrate in the EditAgent form, but multiple data sets can be quite a bit easier to work with.)

Connecting the Combo Box

Combo boxes and list boxes can be bound to data sets just like data grids. List and combo boxes have a DataSource property that can be set to any data set. The DisplayMember property determines which field of the DataSource are displayed. I set the DataSource to theAADataSet (which is the data set corresponding to the Agents_Assignments view). I set the DisplayMember property to Agent_Assignment.CodeName. When the form is displayed, the combo box is automatically populated with all the codenames from the Agent_Assignment view. Because the data is displayed in order, the SelectedIndex property of any code name on the list also refers to the ID of the corresponding agent.

364

Соседние файлы в предмете Программирование на C++