Editing and Updating Data in a Web Forms DataGrid
Số trang: 10
Loại file: pdf
Dung lượng: 29.85 KB
Lượt xem: 12
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
[ Team LiB ] Recipe 7.5 Editing and Updating Data in a Web Forms DataGrid Problem You need to edit complex data using a DataGrid 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
Nội dung trích xuất từ tài liệu:
Editing and Updating Data in a Web Forms DataGrid[ Team LiB ]Recipe 7.5 Editing and Updating Data in a Web Forms DataGridProblemYou need to edit complex data using a DataGrid control and update the database with thechanges made.SolutionBind the results of a database query to a DataGrid control and update the database withchanges made in the DataGrid by configuring the appropriate properties and events.The schema of table TBL00705 used in this solution is shown in Table 7-7. Table 7-7. TBL0705 schema Column name Data type Length Allow nulls?Id int 4 NoIntField int 4 YesStringField nvarchar 50 YesThe Web Forms page sample code defines the DataGrid control with the four columnsthat it contains—Edit or Update/Cancel button, Delete button, Id field, IntField field,StringField field—and the two templates controlling the appearance of data depending onwhether the column is being edited: EditItemTemplate or ItemTemplate. The static Eval() method of the DataBinder class is used to fill the field values in each template. TheContainer.DataItem specifies the container argument for the method which, when used ina data grid, resolves to DataGridItem.DataItem. The code for the Web Forms page isshown in Example 7-9.Example 7-9. File: ADOCookbookCS0705.aspx New Record: ID: Int Field: String Field: The code-behind file contains seven event handlers and three methods:Page.Load Calls the CreateDataSource( ) method and binds data to the Web Forms DataGrid, if the page is being loaded for the first time.CreateDataSource( ) This method fills a DataTable with the TBL0705 table and stores the DataTable to a Session variable to cache the data source for the DataGrid.UpdateDataSource( ) This method creates a DataAdapter and uses it with updating logic generated by a CommandBuilder to update the data source with changes made to the cached DataTable. The updated DataTable is stored to the Session variable used to cache the data source for the DataList.BindDataGrid( ) This method gets the cached data from the Session variable and binds its default view to the DataGrid.DataGrid.CancelCommand Sets the index of the item being edited to -1 to cancel any current editing and calls BindDataGrid( ) to refresh the grid.DataGrid.DeleteCommand Finds and deletes the specified row from the data cached in the Session variable and calls the UpdateDataSource( ) method to persist the change back to the data source. BindDataGrid( ) is called to refresh the grid.DataGrid.EditCommand Sets the index of the item being edited to the index of the row corresponding to the Edit button. This puts that row into edit mode and calls BindDataGrid( ) to refresh the grid.DataGrid.UpdateCommand Finds and updates the specified row in the data cached in the Session variable and calls the UpdateDataSource( ) method to persist the change back to the data source. BindDataGrid( ) is called to refresh the grid.Insert Button.Click Inserts a new row into the data cached in the Session variable and calls the UpdateDataSource( ) method to persist the change back to the data source. BindDataGrid( ) is called to refresh the grid.DataGrid.PageIndexChanged Sets index of the item being edited is to -1 and calls BindDataGrid( ) to refresh the grid.The C# code for the code-behind is shown in Example 7-10.Example 7-10. File: ADOCookbookCS0705.aspx.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Web.UI.WebControls;using System.Data;using System.Data.SqlClient;private const String TABLENAME = TBL0705;// . . .private void Page_Load(object sender, System.EventArgs e){ if(!Page.IsPostBack) { dataGrid.DataSource = CreateDataSource( ); dataGrid.DataKeyField = Id; dataGrid.DataBind( ); }private DataTable CreateDataSource( ){ DataTable dt = new DataTable(TABLENAME); // Create the DataAdapter and fill the table using it. SqlDataAdapter da = new SqlDataAdapter(SELECT * FROM + TABLENAME + ORDER BY Id, ConfigurationSettings.AppSettings[DataConnectString]); da.Fill(dt); da.FillSchema(dt, SchemaType.Source); // Store data in session variable to store data between // posts to server. ...
Nội dung trích xuất từ tài liệu:
Editing and Updating Data in a Web Forms DataGrid[ Team LiB ]Recipe 7.5 Editing and Updating Data in a Web Forms DataGridProblemYou need to edit complex data using a DataGrid control and update the database with thechanges made.SolutionBind the results of a database query to a DataGrid control and update the database withchanges made in the DataGrid by configuring the appropriate properties and events.The schema of table TBL00705 used in this solution is shown in Table 7-7. Table 7-7. TBL0705 schema Column name Data type Length Allow nulls?Id int 4 NoIntField int 4 YesStringField nvarchar 50 YesThe Web Forms page sample code defines the DataGrid control with the four columnsthat it contains—Edit or Update/Cancel button, Delete button, Id field, IntField field,StringField field—and the two templates controlling the appearance of data depending onwhether the column is being edited: EditItemTemplate or ItemTemplate. The static Eval() method of the DataBinder class is used to fill the field values in each template. TheContainer.DataItem specifies the container argument for the method which, when used ina data grid, resolves to DataGridItem.DataItem. The code for the Web Forms page isshown in Example 7-9.Example 7-9. File: ADOCookbookCS0705.aspx New Record: ID: Int Field: String Field: The code-behind file contains seven event handlers and three methods:Page.Load Calls the CreateDataSource( ) method and binds data to the Web Forms DataGrid, if the page is being loaded for the first time.CreateDataSource( ) This method fills a DataTable with the TBL0705 table and stores the DataTable to a Session variable to cache the data source for the DataGrid.UpdateDataSource( ) This method creates a DataAdapter and uses it with updating logic generated by a CommandBuilder to update the data source with changes made to the cached DataTable. The updated DataTable is stored to the Session variable used to cache the data source for the DataList.BindDataGrid( ) This method gets the cached data from the Session variable and binds its default view to the DataGrid.DataGrid.CancelCommand Sets the index of the item being edited to -1 to cancel any current editing and calls BindDataGrid( ) to refresh the grid.DataGrid.DeleteCommand Finds and deletes the specified row from the data cached in the Session variable and calls the UpdateDataSource( ) method to persist the change back to the data source. BindDataGrid( ) is called to refresh the grid.DataGrid.EditCommand Sets the index of the item being edited to the index of the row corresponding to the Edit button. This puts that row into edit mode and calls BindDataGrid( ) to refresh the grid.DataGrid.UpdateCommand Finds and updates the specified row in the data cached in the Session variable and calls the UpdateDataSource( ) method to persist the change back to the data source. BindDataGrid( ) is called to refresh the grid.Insert Button.Click Inserts a new row into the data cached in the Session variable and calls the UpdateDataSource( ) method to persist the change back to the data source. BindDataGrid( ) is called to refresh the grid.DataGrid.PageIndexChanged Sets index of the item being edited is to -1 and calls BindDataGrid( ) to refresh the grid.The C# code for the code-behind is shown in Example 7-10.Example 7-10. File: ADOCookbookCS0705.aspx.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Web.UI.WebControls;using System.Data;using System.Data.SqlClient;private const String TABLENAME = TBL0705;// . . .private void Page_Load(object sender, System.EventArgs e){ if(!Page.IsPostBack) { dataGrid.DataSource = CreateDataSource( ); dataGrid.DataKeyField = Id; dataGrid.DataBind( ); }private DataTable CreateDataSource( ){ DataTable dt = new DataTable(TABLENAME); // Create the DataAdapter and fill the table using it. SqlDataAdapter da = new SqlDataAdapter(SELECT * FROM + TABLENAME + ORDER BY Id, ConfigurationSettings.AppSettings[DataConnectString]); da.Fill(dt); da.FillSchema(dt, SchemaType.Source); // Store data in session variable to store data between // posts to server. ...
Tìm kiếm theo từ khóa liên quan:
công nghệ thông tin kỹ thuật lập trình Oreilly Ado Dot Net Cookbook Ebook-Lib Editing and Updating Data in a Web Forms DataGridTài liệu liên quan:
-
52 trang 434 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 321 0 0 -
74 trang 304 0 0
-
96 trang 299 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 293 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 286 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 277 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 270 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 270 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0