Danh mục

Edit and Update Using Data Bound Controls

Số trang: 7      Loại file: pdf      Dung lượng: 31.63 KB      Lượt xem: 15      Lượt tải: 0    
Thư viện của tui

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

1.4 Edit and Update Data Using Bound Controls Although viewing data is fine in some situations, the real power comes in being able to edit the data in the text box and update the data in the server
Nội dung trích xuất từ tài liệu:
Edit and Update Using Data Bound Controls 1.4 Edit and Update Data Using Bound ControlsAlthough viewing data is fine in some situations, the real power comes in being able toedit the data in the text box and update the data in the server. In this How-To, you learnjust that, using some new methods and properties from the various data controls,including autogenerated commands that create update, insert, and delete SQL statementsfor you. This How-To also introduces you to the BinderContext class, which is used toperform updates with bound controls.The form as created in How-To 1.4 is great if you just want to view data for variousrecords. But what if you want to let users edit and update the data?TechniqueContinuing with the form you used in the previous How-Tos, you are going to add a fewcommand buttons with code that will perform the following: • Edit. Toggle the look of the text boxes to let the user know that he can edit the data in the controls by giving the controls a sunken look. • Save. Save the changes made to the data back to the server. Then toggle the look of the text boxes back to flat so that the user knows he cant edit them. • Cancel. Toggle the text boxes back to flat so that the user knows he cant edit them anymore, but dont save the data.It is important to let users know when they can edit and when they cant. By toggling thestyle of the text boxes between flat and sunken and the color between gray and white,users have a definite clue that they can edit the data at certain times (see Figure 1.8).Another useful item to note about this How-To is the introduction of the BindingContextclass. This class helps you work with data bound controls. Each control on a Windowsform has a BindingContext object as well as BindingContext objects for any controls thatare contained within that control. Each form also has a BindingContext object.BindingContext objects manage BindingManagerBase class object(s) for each control.Through the BindingContext object, you can manage editing and updating data back tothe server. BindingManagerBase objects help with synchronization of datasets withcontrols. You are going to use the BindingContext class throughout the rest of thischapter, but only a couple of methods.StepsOpen the solution for the chapter called VB .NET How-To Chapter 1, and run theapplication. From the main form, click the command button with the caption How-To1.4. Click the Load List button. Along with the list being filled with customers whosenames start with A, you will see the detail How-To fill in on the right side of the formwith the first customer in the list. Click Edit. You will now be able to change the data inthe text boxes. After you have changed a couple of fields, click Save. If you selectanother customer from the list box, select the one you changed. You will see that yourchanges have in fact been saved. If you click Cancel instead of Save, your changes willnot be saved. Note that if you change the name of the customer, you must load the listagain to see the changes in the list box. 1. Add the three command buttons to your form as described in Table 1.6 and as displayed in Figure 1.8. Table 1.6. Command Buttons to Edit, Save, and Cancel Changes to Data Object Property Setting Command Button Name btnEdit Caption &Edit Command Button Name btnSave Caption &Save Command Button Name btnCancel Caption &Cancel 2. Add the code shown in Listing 1.6 to the btnEdit Click event. Listing 1.6 frmHowTo1_4.vb: Calling the ActiveEditing Subroutine from the btnEdit Command Button Private Sub btnEdit_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnEdit.Click - Enable the editing of the form ActivateEditing(True) End Sub This calls the routine called ActivateEditing, listed here. This code iterates through all the controls on the form and uses the TypeOf condition to check whether the current control is a text box. It also ensures that the control is not the txtCustLimit text box. If the control is a text box and the parameter called bEnable has been passed in as True, then the sunken look is given to the text box using theBorderStyle and the BackColor property is set to white. Otherwise, theBorderStyle is set to FixedSingle, which is flat, and the BackColor is set to thebackcolor of the form. The Enabled property of the text box is set to whateverbEnabled is: True ...

Tài liệu được xem nhiều: