Danh mục tài liệu

Add, Edit, and Delete Data Using the DataGrid Control

Số trang: 14      Loại file: pdf      Dung lượng: 45.23 KB      Lượt xem: 15      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:

5,7 Thêm, Sửa Đổi, và Xóa dữ liệu Sử dụng DataGrid Control Bảng điều khiển và DataRepeater cũng tốt khi tôi đã tập hợp dữ liệu nhỏ, nhưng màn hình hiển thị chỉ cần giữ đi và về, và tôi phải viết một loạt các mã để thay đổi phân loại thứ tự của dữ liệu
Nội dung trích xuất từ tài liệu:
Add, Edit, and Delete Data Using the DataGrid Control 5.7 Add, Edit, and Delete Data Using the DataGrid ControlThe Table controls and DataRepeater are fine when I have small sets of data, but thedisplay just keeps going on and on, and I have to write a bunch of code to change the sortorder of the data. How do I create a table-like display that will show a set number of rowsat a time and let me sort the data?TechniqueOne of the nice things about the DataGrid control that makes it so much more powerfulthan the other list controls is its ability to add, edit, and delete directly within the control.This How-To shows you how to create columns to manage your data using the DataGridcontrol.Adding Buttons to the DataGrid ControlYou will use the DataGrid control with more code this time so that you can work withdata more. You will also be adding a couple of buttons to the DataGrid display to allowyou to edit, delete, and update data.You will add buttons to the data grid by right-clicking on the control, choosing PropertyBuilder, and then choosing the Columns tab. You can then select from the list of buttontypes (see Figure 5.12).Figure 5.12. The buttons listed in Selected Columns are being used for this example.Note Be sure to leave the Create Columns Automatically at Run Time check box checked. Unless you specify other columns or have the data loaded at run-time, you will end up with just the buttons, which would be pretty boring.Using Events with Buttons on the Data GridAfter you have selected to include buttons in the data grid, you not only have to add thecode to the events for the specific buttons, but you also have to make sure that ASP.NETknows the events to use. You can do this in a couple of ways.One way is to set the AutoEventWireUp attribute of the page to True. You can see thisattribute in the first line of the Web page. By default, the attribute is set to False.After you add the buttons to the data grid, you will see the buttons in the DataGridcontrol.However, a couple of disadvantages result from setting the AutoEventWire: • You have to use the required names for your events. • Events sometimes end up being called twice on the form.Microsoft recommends not setting the AutoEventWireUp to True.The other alternative is to add the events and HTML tags yourself. You will see theHTML code added in step 3.Loading a Schema into a Data TableAnother option that is introduced in this How-To is the fill of a data table by using theFillSchema method of the data adapter. This means that the data table will be smart andknow what the constraints and properties of the columns are before you try to save thedata back to the server. The big benefit with using the FillSchema method with theDataGrid control is that it will be intelligent, and it wont let a user try to edit an autoincrement column, such as an identity column. It will make such a column disabled.When you update the columns from the data grid back into the data table, you can alsocheck to see if the columns AutoIncrement property is True. Unless you use theFillSchema method, the AutoIncrement property will come back False, even if the actualcolumn in the table on the server has it set to True.Additional Objects, Properties, and Methods Used to Handle DataIn addition to the FillSchema method of the data adapter, you will also use the Updatemethod, which will call the Update, Delete, or Append statement, depending on the taskthat is being performed.The CommandBuilder object will generate your SQL statements used to sendmodifications back to the server.In addition to the methods mentioned for the data adapter, you will use some propertiesand methods of the data table to add, edit, and delete data. You can see a list of thoseobjects, properties, and methods in Table 5.10. Table 5.10. Objects, Properties, and Methods of the DataTable ObjectObject Property/Method DescriptionDataTable.Rows Delete Deletes a row from the DataTable object (the data is not deleted from the server at this point).DataTable.Rows Count Returns the number of rows that are currently in the DataTable object.DataTable NewRow Creates a new DataRow object.DataTable.Rows Add Adds the DataRow object to the DataTable object.DataTable BeginLoadData Turns off the schema checking that occurs when youre adding the new row to the DataTable object.DataTable AcceptChanges Resets the DataTable status, including ending edits on DataRow objects. The DataRowState also changes. All added and modified rows become unchanged, and deleted rows are removed.DataTable RejectChanges Rejects the changes made since the last AcceptChanges was called. Lets you roll back changes if errors occur.StepsOpen and run the Visual Basic .NET-Chapter 5 solution. From the main page, click onthe hyperlink with the caption How-To 5.7: Add, Edit, and Delete Data Using theDataGrid Control. You will then see all the regions loaded into a data grid. You can clickon the Edit button to edit data, and so on. You can see the form created in Design view inFigure 5.13. 1. Create a Web Form. Then place the controls in Table 5.11 and Figure 5.13 with the following properties set. Dont worry about the Edit and Delete buttons dis ...