Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The GridView control combined with the SqlDataSource control can be used to create a very elegant editing interface where the user sees data listed in an HTML table and then can click a hyperlink to open just the selected row for editing. From there, you can then allow the user to make changes to the data, which will automatically be saved to the server-side database. Most of the code that you need for this is already generated when you drag a GridView control into a page within Visual Web Developer. In this demonstration, I'll show you how to modify the code to turn editing on and then how to block editing of particular columns. I'll work with the file AuthorsWithEditing.aspx and then look at the file in Design View. You'll see that it has a version of the Authors GridView, this time displaying just three columns, the Last Name, First Name and Phone Number.
First, I'll turn Editing on, on the GridView control. I'll click on the GridView control and then open the list of available tasks and I'll select Enable Editing. When I select that option that changes a single property of the GridView control. Then I'll save my changes by pressing Ctrl+S and I'll run the page in the browser. When the GridView is displayed now, it shows a new column with a hyperlink next to each row. I'll go down to a particular page, let's say page number 2 and I'll click the Edit link for the first row currently displayed. You'll see that that results in opening up the row for editing, displaying text input controls for each of the columns.
I'll make certain changes to the columns. For example, I'll change Ann's name and add an "e" at the end and then I'll click the Update link. That results in saving the data all the way back to the server-side database. I can prove that the data was actually changed by continuing to navigate around in the pages and you'll see that the updated change has taken or is currently displayed. Now in certain conditions, you may want to allow editing for one column but not others. When you turn editing on for the entire GridView, by default, all columns are editable. You can then go into individual bound fields and turn on a property called ReadOnly.
When you set the ReadOnly property to True for a particular bound field and then you click the Edit link for a row, the columns marked as ReadOnly will be displayed as text rather than displayed with an input control. And then there are some changes you'll need to make in the underlying definition of the SqlDataSource object to make it compatible with your changes to the GridView. Close the browser and return to Visual Web Developer and with the GridView still selected I'll open the list of available tasks and click Edit Columns. Now, let's say that I want the user to be able to edit the phone number, but not the last name or first name. So I'll click the Last Name field in the list of Selected Fields and then I'll go to the Behavior category and locate the ReadOnly property and change it to True.
I'll do the same thing for the First Name. I'll click on the field in the list of Selected Fields. Locate the ReadOnly property and set it to True and then I'll click OK. I'll press Ctrl+S to save my changes and then select Debug > Start Without Debugging to run the page. Now this time when I click the Edit link, you'll see that only the phone number is displayed as an input control. If I cancel, my changes aren't saved. But here is that problem that you'll run into and I'll show you the solution in a moment. I'll click the Edit link. I'll make a small change to the phone number and then click the Update link and I'll see a big old ASP.NET error displayed on the screen.
The problem that you're encountering is shown down here. The message is Cannot insert the value Null into column au_ lname. The problem that you're running into is that the GridView control passes in what's called a Null value or the absence of a value into the Last Name column. You can solve this problem by modifying the structure of the Update SQL statement that's being executed when the page is updated. I'll close the browser and return to Visual Web Developer and then I'll look at the page in Source view and I'll look at it in full screen, so I can see as much code as possible. I'll press Alt+ Shift+Enter. Now, I'll scroll down to the SqlDataSource definition. The problem is here in the UpdateCommand attribute.
The Update statement is expecting to set the values of all of the columns in the backend database table. But I'm only passing in a value for the phone number. So I'm going to reduce the SQL statement. I'll get rid of the column value pair for au_lname. I'll do the same thing for au_fname and its trailing comma. I'll leave the phone number in, but then I'll place the cursor right before the comma after the phone number. I'll hold down the Shift key and then start cursoring over to select as much of the SQL statement as I need to delete and I'll finish selecting text right before the keyword WHERE. Then I'll press the Delete key.
Here is the remaining SQL statement. Update authors. Set phone equal to the phone parameter where the au_id column equals the au_id parameter. So, that's how the Update statement will match the structure of the GridView. I'm only going to be passing in that one value. So I don't need to set up the SQL statement to update more than that column. Then I'll go to the UpdateParameters section of the SqlDataSource definition and I'll do the same thing, this time for the parameters which are being filled in automatically from the GridView. I'll remove the au_lname and au_fname parameters and also remove address, city, state and zip. I'll leave the phone and the Author ID, which are both needed.
Now, I'll save my changes and I'll run the page in the browser once again, selecting Debug > Start Without Debugging. I'll click the Edit link. Once again, I'm only able to edit the phone number because I set the other columns as ReadOnly. I'll make a change to the Phone number. I'll change it to have a trailing number of 1111. I'll click the Update link and you'll see that the data is successfully saved to the database on the server and displayed in the updated page. I can now page forward and back through the data display and you'll see that the data has been saved successfully and is being displayed on the refreshed page.
So, to review the steps for setting up an updatable GridView, in Design View, you turn editing on by selecting the task list and selecting Enable Editing. Then you edit the columns. Set ReadOnly to True for each column you don't want the user to edit. Then go into the Source View and modify the SqlDataSource definition, modify the SQL statement for updating the table so it's only affecting the columns that the user is allowed to edit and then remove the parameters for those columns that are ReadOnly as well. Then you'll be allowing the user to modify the data directly through the GridView with minimal programming on your part.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98164 Viewers
61 Video lessons · 85466 Viewers
71 Video lessons · 69427 Viewers
56 Video lessons · 101780 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.