8.7. Modifying and Updating Data in a Web Forms GridView Control
Problem
You need to edit data using a GridView control and update the database with the changes made.
Solution
Bind the results of a database query to a DataGrid control and update the database with changes made in the DataGrid using a data adapter.
Follow these steps:
The solution uses a table named
GridViewin theAdoDotNet35Cookbookdatabase.Execute the following T-SQL statement to create the table:
USE AdoDotNet35Cookbook GO CREATE TABLE GridView( Id int NOT NULL PRIMARY KEY, IntField int NULL, StringField nvarchar(50) NULL )
The schema of table
GridViewused in this solution is shown in Table 8-9.Table 8-9. GridView table schema
Column name
Data type
Length
Allow nulls?
Idint4
No
IntFieldint4
Yes
StringFieldnvarchar50
Yes
Execute the following T-SQL batch to create the sample data required by the solution:
USE AdoDotNet35Cookbook GO INSERT INTO GridView VALUES (1, 10, 'Field1.1') INSERT INTO GridView VALUES (2, 20, 'Field1.2') INSERT INTO GridView VALUES (3, 30, 'Field1.3') INSERT INTO GridView VALUES (4, 40, 'Field1.4')
Create a C# ASP.NET web application named
UpdateDataWebFormGridView.Add the following control to the Default.aspx design surface:
GridViewnamedgridView
The completed layout of the Web Form page Default.aspx is shown in Figure 8-13.

Figure 8-13. Layout for Default.aspx in UpdateDataWebFormGridView solution
The code ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access