[ Download Code ]
The GridView control will not allow you to add new rows to a data source, but the DetailsView (another new control in ASP.NET 2.0) will. While the GridView can display multiple rows, the DetailsView control displays only one row at a time, but the DetailsView will allow you to edit, delete, and add rows to a data source.
Using the same web form, drag and drop a second AccessDataSource control and a DetailsView control onto the web form. Now we just set a few properties. You need a second AccessDataSource control because you are going to set a filter expression on it so that it only displays one row (the row you select). Microsoft recommends using a separate DataSource control with each data control even if you are not using a filter.
Set the properties of the second AccessDataSource control are the same as the first with one exception and three changes:
- DataFile: data\pubs.mdb
- SelectCommand: "select * from [authors]"
- DeleteCommand: "DELETE FROM [authors] WHERE [au_id] = ?"
- UpdateCommand: "UPDATE [authors] SET [au_lname] = ?, [au_fname] = ?, [phone] = ?, [address] = ?, [city] = ?, [state] = ?, [zip] = ?, [contract] = ? WHERE [au_id] = ?"
- InsertCommand: "INSERT INTO [authors] ([au_id], [au_lname], [au_fname], [phone], [address], [city], [state], [zip], [contract]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
- FilterExpression: au_id='@auid' (Make sure to include single quotes)
- FilterParameters (Add Parameter)
- Parameter source: Control
- ControlID: GridView1
- Name: auid
Set the properties of the GridView control:
- AutoGenerateEditButton: False
- AutoGenerateDeleteButton: False
- AutoGenerateSelectButton: True
Set the properties of the DetailsView control:
- AutoGenerateEditButton: True
- AutoGenerateDeleteButton: True
- AutoGenerateInsertButton: True
Save your web form and place the database file in the "data" sub-directory of the same virtual directory. Remember this code sample requires ASP.NET 2.0. Using four web server controls we now have a web page that will edit, delete, and add rows to an Access database table. All of this in just a few minutes. Wow! When you click the select button on the GridView, that row will be displayed in the DetailsView. You can then edit or delete that row. You can also add a new row from the DetailsView.
Screen Shot 2 shows the revised web form in action. Listing 3 shows the web form. Don't panic! It contains a small amount of code. The code is not necessary for the web form to function, but it sure adds a nice feature. When you make changes in the DetailsView they are not updated in the GridView. To fix this we just bind the GridView to the database table every time we make a change. I'm sorry, but this requires a small amount of code.
In Source view add the following properties to the DetailsView:
Then add the sub routines as show in Listing 3. In each we simply bind the database table to the GridView so that it reflects the changes made in the DetailsView. The code is not required, but without this code, you would have to sort or change pages in the GridView to see the changes.
In conclusion, let me say that the new controls in ASP.NET 2.0 are going to make editing Access databases much easier. I also look forward to using the rest of these new controls to rapidly develop web applications.