Creating a Master/Detail View App

Một phần của tài liệu Visual C 2012 How to Program _ www.bit.ly/taiho123 (Trang 920 - 924)

2. When the app executes, another compiler (known as the just-in-time compiler

22.8 Creating a Master/Detail View App

The range variablebookin the nested query iterates over the current author’s books using in theTitles property. The Title1 property of a given bookreturns theTitle column from that row of theTitlestable in the database.

The nestedforeach statements (lines 71–81) use the properties of the anonymous type created by the query to output the hierarchical results. The outer loop displays the author’s name and the inner loop displays the titles of all the books written by that author.

22.8 Creating a Master/Detail View App

Figure 22.28 demonstrates a so-calledmaster/detail view—one part of the GUI (the master) allows you to select an entry, and another part (the details) displays detailed information about that entry. When the app first loads, it displays the name of the first author in the data source and that author’s books (Fig. 22.28(a)). When you use the buttons on theBinding-

Navigatorto change authors, the app displays the details of the books written by the corre- sponding author (Fig. 22.28(b)). This app only reads data from the entity data model, so we disabled the buttons in theBindingNavigatorthat enable the user to add and delete records.

70 // display titles written by each author, grouped by author 71 foreach ( var author in titlesByAuthor )

72 {

73 // display author's name

74 outputTextBox.AppendText( "\r\n\t" + author.Name + ":" );

75

76 // display titles written by that author

77 foreach ( var title in )

78 {

79 outputTextBox.AppendText( "\r\n\t\t" + title );

80 } // end inner foreach 81 } // end outer foreach

82 } // end method JoiningTableData_Load 83 } // end class JoiningTableData

84 } // end namespace JoinQueries

Fig. 22.28 | Master/Detailapp. (Part 1 of 2.)

Fig. 22.27 | Getting a list of titles grouped by authors. (Part 2 of 2.)

author.Titles

a)Master/Detailapp displaying books for the first author in the data source

When you run the app, experiment with theBindingNavigator’s controls. The DVD-play- er-like buttons of theBindingNavigatorallow you to change the currently displayed row.

22.8.1 Creating the Master/Detail GUI

You’ve seen that the IDE can automatically generate theBindingSource,BindingNavi- gatorand GUI elements when you drag a data source onto theForm. You’ll now use two

BindingSources—one for the master list of authors and one for the titles associated with a given author. Both will be generated by the IDE.

Step 1: Creating the Project

Follow the instructions in Section 22.5.2 to create and configure a newWindows Forms Application project called MasterDetail. Name the source file Details.csand set the

Form’sTextproperty toMaster/Detail.

Step 2: Adding a Data Source for theAuthorsTable

Follow the steps in Section 22.5.3 to add a data source for theAuthorstable. Although you’ll be displaying records from theTitlestable for each author, you do not need to add a data source for that table. The title information will be obtained from theTitlesnavi- gation property in theAuthorentity data model class.

Step 3: Creating GUI Elements

Next, you’ll use theDesignview to create the GUI components by dragging-and-dropping items from theData Sourceswindow onto theForm. In the earlier sections, you dragged an object from theData Sourceswindow to theFormto create aDataGridView. The IDE allows you to specify the type of control(s) that it will create when you drag and drop an object from theData Sourceswindow onto aForm. To do so:

1. Switch toDesignview for theDetailsclass.

2. Click theAuthornode in theData Sourceswindow—it should change to a drop- down list. Open the drop-down by clicking the down arrow and select theDetails Fig. 22.28 | Master/Detailapp. (Part 2 of 2.)

b)Master/Detailapp displaying books for the third author in the data source

22.8 Creating a Master/Detail View App 881

option—this indicates that we’d like to generateLabel–TextBoxpairs that repre- sent each column of theAuthorstable.

3. Drag theAuthornode from theData Sourceswindow onto theForminDesignview.

This creates theauthorBindingSource, theauthorBindingNavigatorand theLa-

bel–TextBoxpairs that represent each column in the table. Initially, the controls appear as shown in Fig. 22.29. We rearranged the controls as shown in Fig. 22.28.

4. By default, theTitles navigation property is implemented in the entity data model classes as aHashSet<Title>. To bind the data to GUI controls properly, you must change this to anObservableCollection<Title>. To do this, expand the class library project’sBooksModel.edmxnode in theSolution Explorer, then ex- pand theBooksModel.ttnode and openAuthor.csin the editor. Add ausing statement for the namespaceSystem.Collections.ObjectModel. Then, in the

Authorconstructor changeHashSet toObservableCollection. Right click the class library project in theSolution Explorerand selectBuildto recompile the class.

5. Next, click theTitlesnode that’s nested in theAuthornode in theData Sources

window—it should change to a drop-down list. Open the drop-down by clicking the down arrow and ensure that theDataGridViewoption is selected—this is the GUI control that will be used to display the data from theTitlestable that cor- responds to a given author.

6. Drag theTitlesnode onto theForminDesignview. This creates thetitlesBind- ingSourceand theDataGridView. This control is only forviewingdata, so set its

ReadOnlyproperty toTrueusing thePropertieswindow. Because we dragged the

Titlesnode from theAuthornode in theData Sourceswindow, theDataGridView

will automatically display the books for the currently selected author once we bind the author data to theauthorBindingSource.

We used theDataGridView’sAnchorproperty to anchor it to all four sides of theForm. We also set theForm’sSizeandMinimumSizeproperties to550, 300to set theForm’s initial size and minimum size, respectively. The completed GUI is shown in Fig. 22.30.

22.8.2 Coding the Master/Detail App

The code to display an author and the corresponding books (Fig. 22.31) is straightforward.

Lines 17–18 create theDbContext. TheForm’sLoadevent handler (lines 22–32) orders the

Authorobjects byLastName(line 26) andFirstName(line 27), then loads them into mem- ory (line 28). Next, line 31 assigns dbcontext.Authors.Local to the authorBinding- Source’sDataSourceproperty. At this point:

• theBindingNavigatordisplays the number ofAuthorobjects and indicates that the first one in the results is selected,

Fig. 22.29 | Detailsrepresentation of anAuthor.

• theTextBoxes display the currently selectedAuthor’sAuthorID,FirstNameand

LastNameproperty values, and

• the currently selectedAuthor’s titles are automatically assigned to thetitlesBind- ingSource’sDataSource, which causes theDataGridViewto display those titles.

Now, when you use theBindingNavigatorto change the selectedAuthor, the correspond- ing titles are displayed in theDataGridView.

Fig. 22.30 | Finished design of theMaster/Detailapp.

1 // Fig. 22.31: Details.cs

2 // Using a DataGridView to display details based on a selection.

3 using System;

4 using System.Data.Entity;

5 using System.Linq;

6 using System.Windows.Forms;

7

8 namespace MasterDetail 9 {

10 public partial class Details : Form

11 {

12 public Details()

13 {

14 InitializeComponent();

15 } // end constructor 16

17 // Entity Framework DbContext

18 BooksExamples.BooksEntities dbcontext = 19 new BooksExamples.BooksEntities();

20

Fig. 22.31 | Using aDataGridViewto display details based on a selection. (Part 1 of 2.)

Một phần của tài liệu Visual C 2012 How to Program _ www.bit.ly/taiho123 (Trang 920 - 924)

Tải bản đầy đủ (PDF)

(1.020 trang)