Danh mục

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    
Thư viện của tui

Hỗ trợ phí lưu trữ khi tải xuống: 4,000 VND Tải xuống file đầy đủ (10 trang) 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. ...

Tài liệu được xem nhiều: