You will use the BindingContext object again to add a new record by using the AddNew method.. This button will have code behind it that will use the RemoveAt method of the ContextBinding
Trang 11.5 Add and Delete Records Using Bound Controls
Besides editing data, adding new records and deleting records in a table are obviously important features This How-To shows you just that, taking advantage of the
BinderContext class
Besides being able to edit current records that already exist, users need to be able to add and delete records as well How do you add and delete records using bound controls?
Technique
To create the Add New Record feature, you will add a button called btnAddNew that has code behind it You will use the BindingContext object again to add a new record by using the AddNew method A module variable will be added to the form to keep track of whether a record is being added or edited If a new record is being added, then when the update is performed, the application needs to reload the data in the list box in case the new record's CompanyName data falls into the chosen list limit that is specified in the txtCustLimit text box
To create the Delete Record feature, you will add a button called btnDelete This button will have code behind it that will use the RemoveAt method of the ContextBinding object
to remove the record from the dataset The data will then be posted back to the server
Steps
Open the solution for the chapter called "VB NET How-To Chapter 1," and run the application From the main form, click on the command button labeled How-To 1.5 Click the Load List button Click the button labeled New Enter the text ALF for
Customer ID and Alfred's Fried Foods in the Company Name Then click the button labeled Save, and see the record you just entered added to the list After the record has been entered, you can test the delete feature by clicking on the button named Delete The record goes away, and the list is rebuilt
1 Add a new command button under btnEdit, and then set the Name property to btnNew and the Caption property to &Save
2 Add a new module-level variable called mbAddNew, which is a Boolean variable
to keep track of whether you are adding a new record This variable will be placed outside of your routines, just under the forms code You can see this in Figure 1.9
Trang 2Figure 1.9 By placing this variable just underneath Windows Form Designer Generated Code and declaring it Private, routines inside the Class can see it,
but those outside cannot
Tip
You can collapse and expand routines that you are working on within modules This makes it handy when you are working on new routines (which can be expanded) and don't want to mess with old routines (which can be collapsed)
3 Add the following code to the Click event of the new command button btnNew This code first sets the Boolean variable called mbAddNew to True It then uses the AddNew method of the form's BindingContext object to add a new record to the dsCustomerIndividual dataset Finally, the code calls the ActiveEditing routine
to enable the text boxes
Listing 1.11 frmHowTo1_5.vb: Adding a New Record to the
dsCustomerIndividual Dataset and Toggling Text Boxes
Private Sub btnNew_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnNew.Click
Trang 3mbAddNew = True
'- Using the BindingContext class add a new record
Me.BindingContext(Me.dsCustomerIndividual, "Customers").AddNew() ActivateEditing(True)
End Sub
4 Modify the Click event of the btnSave to test whether the mbAddNew flag has been set to True, meaning that a new record is being added If True, then after saving the record by calling the SaveRecord and ActivateEditing routines, the LoadList and RefreshIndividual are called to load the new record's data Note that the SaveRecord, ActiveEditing, and RefreshIndividual routines have not changed from How-To 1.4 The mbAddNew variable is then set to False
Listing 1.12 frmHowTo1_5.vb: Saving the Changed Data, Toggling Text Boxes, and Reloading the List Box and First Record in the List
Private Sub btnSave_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnSave.Click
'- Save the information
SaveRecord()
'- Disable the text boxes
ActivateEditing(False)
If mbAddNew Then
LoadList()
RefreshIndividual()
mbAddNew = False
End If
End Sub
5 Modify the Click event for the btnCancel button to reset the mbAddNew variable
to False, as seen in Listing 1.13
Listing 1.13 frmHowTo1_5.vb: Canceling the Edit and Resetting the
mbAddNew Variable
Trang 4Private Sub btnCancel_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCancel.Click
'- Use the BindingContext class to cancel the current editing
Me.BindingContext(Me.dsCustomerIndividual,
"Customers").CancelCurrentEdit()
ActivateEditing(False)
mbAddNew = False
End Sub
6 Now it is time to add the delete functionality To do this, add the following code to the Click event of the new button called btnDelete The first line of the code you added begins by introducing a new method called RemoveAt and a new property called Position, both used with the BindingContext object You will work from the inside out The Position property tracks the current position in the dataset, in this case dsCustomerIndividual The RemoveAt method marks the record that is at the position passed to it for deletion
Next, the Update method of the odaCustomerIndividual adapter is used to call the DELETE SQL statement command against the dataset, deleting the actual rows in the data set The AcceptChanges method is called to send the changes to the
dataset, a delete in this case, back to the server Finally, the LoadList,
RefreshIndividual, and ActivateEditing subroutines are called to refresh the list, refresh the first record in the list for the text boxes, and toggle the text boxes so that the user knows he can't edit them
Listing 1.14 frmHowTo1_5.vb: Deleting the Selected Record and Reloading the List Box
Private Sub btnDelete_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnDelete.Click
'- Mark the row for deletion using the RemoveAt method of the
BindingContext
Me.BindingContext(Me.dsCustomerIndividual, _
"Customers").RemoveAt(Me.BindingContext(Me.dsCustomerIndividual, "Customers").Position)
'- Perform the requested task at the dataset
' level using the data adapter
odaCustomerIndividual.Update(dsCustomerIndividual, "Customers")
Trang 5'- By accepting the changes, the data gets sent back to the server
dsCustomerIndividual.AcceptChanges()
'- Reload the list
LoadList()
'- Display the first record
RefreshIndividual()
'- Disable the text boxes
ActivateEditing(False)
End Sub
How It Works
When the user clicks the btnNew button, a flag (mbAddNew) is set to True, the dataset is set for adding a record with the AddNew method of the BindingContext, and the text boxes are enabled for editing
If the user then clicks the btnSave button, the data is updated to the dataset and then back
to the server If the user clicks the btnCancel button, the edits are canceled In both cases, the mbAddNew variable is set to False and the ActivateEditing routine is called to disable the text boxes and let the user know that the text boxes are not available for editing Finally, if the btnSave button was clicked and the mbAddNew was set to True, the
LoadList and RefreshIndividual routines are called
When the user clicks the btnDelete button, the record is deleted from the recordset and then from the server The list box is reloaded and the first record in the list is displayed in the text boxes The text boxes are then disabled
Comments
Here you have the basic commands needed to create a full-fledged data entry form You can add, edit, and delete information from the form This actually takes less code than if you use Visual Basic 6.0
The BindingContext object really goes a long way to helping you work with bound data