Using a DataView to Control Edits, Deletions, or Additions in Windows Forms
Số trang: 3
Loại file: pdf
Dung lượng: 14.63 KB
Lượt xem: 10
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.14 Using a DataView to Control Edits, Deletions, or Additions in Windows Forms Problem You need to selectively prevent users from editing, deleting, or adding data in a Windows Forms application. Solution Bind a DataView to Windows Forms controls. The sample code contains four event handlers
Nội dung trích xuất từ tài liệu:
Using a DataView to Control Edits, Deletions, or Additions in Windows Forms[ Team LiB ]Recipe 7.14 Using a DataView to Control Edits, Deletions, or Additions in WindowsFormsProblemYou need to selectively prevent users from editing, deleting, or adding data in a WindowsForms application.SolutionBind a DataView to Windows Forms controls.The sample code contains four event handlers:Form.Load Sets up the sample by filling a DataTable with the Orders table from the Northwind sample database. A DataView is created from the table and bound to the data grid on the form.Allow Delete Button.Click Sets whether the DataView allows records to be deleted based on the value in a check box.Allow Edit Button.Click Sets whether the DataView allows records to be edited based on the value in a check box.Allow Insert Button.Click Sets whether the DataView allows records to be inserted based on the value in a check box.The C# code is shown in Example 7-30.Example 7-30. File: ControlDataEditWithDataViewForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Data;using System.Data.SqlClient;private DataView dv;// . . .private void ControlDataEditWithDataViewForm_Load(object sender, System.EventArgs e){ // Fill the Order table. SqlDataAdapter da = new SqlDataAdapter(SELECT * FROM Orders, ConfigurationSettings.AppSettings[Sql_ConnectString]); DataTable dtOrders = new DataTable(Orders); da.FillSchema(dtOrders, SchemaType.Source); da.Fill(dtOrders); // Create a view and bind it to the grid. dv = new DataView(dtOrders); dataGrid.DataSource = dv;}private void allowDeleteCheckBox_CheckedChanged(object sender, System.EventArgs e){ dv.AllowDelete = allowDeleteCheckBox.Checked;}private void allowEditCheckBox_CheckedChanged(object sender, System.EventArgs e){ dv.AllowEdit = allowEditCheckBox.Checked;}private void allowInsertCheckBox_CheckedChanged(object sender, System.EventArgs e){ dv.AllowNew = allowInsertCheckBox.Checked;}DiscussionThe DataGrid control does not have properties that control the adding, editing, or deletingthe data in the control. Binding a DataGrid to a DataTable binds to the default view of theunderlying DataTable.The DataView class represents a view of the DataTable that can be data bound on bothWindows Forms and Web Forms. The DataView can be customized for editing, filtering,searching, and sorting.The DataView class can be used to add, edit, or delete records in the underlyingDataTable. The properties described in Table 7-14 control the data modificationpermitted in a DataView. Table 7-14. DataView properties Property DescriptionAllowDelete Gets or sets a Boolean value indicating whether deletes are allowedAllowEdit Gets or sets a Boolean value indicating whether edits are allowedAllowNew Gets or sets a Boolean value indicating whether new rows can be addedIf AllowNew is true, the record is not added until the EndEdit( ) method is called eitherexplicitly or implicitly. The CancelEdit( ) method of the DataRowView can be called todiscard the row before it is added.If AllowEdit is true, the changes to the row are not committed until the EndEdit( )method is called either explicitly or implicitly. Only one row can be edited at a time. TheCancelEdit( ) method of the DataRowView can be called to discard the row before it isadded.If the AddNew( ) or BeginEdit( ) method of the DataRowView is called, EndEdit( ) iscalled implicitly on the pending row; this applies the changes to the row in the underlyingDataTable. In data controls that allow editing multiple records, EndEdit( ) is calledimplicitly when the current row is changed.[ Team LiB ]
Nội dung trích xuất từ tài liệu:
Using a DataView to Control Edits, Deletions, or Additions in Windows Forms[ Team LiB ]Recipe 7.14 Using a DataView to Control Edits, Deletions, or Additions in WindowsFormsProblemYou need to selectively prevent users from editing, deleting, or adding data in a WindowsForms application.SolutionBind a DataView to Windows Forms controls.The sample code contains four event handlers:Form.Load Sets up the sample by filling a DataTable with the Orders table from the Northwind sample database. A DataView is created from the table and bound to the data grid on the form.Allow Delete Button.Click Sets whether the DataView allows records to be deleted based on the value in a check box.Allow Edit Button.Click Sets whether the DataView allows records to be edited based on the value in a check box.Allow Insert Button.Click Sets whether the DataView allows records to be inserted based on the value in a check box.The C# code is shown in Example 7-30.Example 7-30. File: ControlDataEditWithDataViewForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Data;using System.Data.SqlClient;private DataView dv;// . . .private void ControlDataEditWithDataViewForm_Load(object sender, System.EventArgs e){ // Fill the Order table. SqlDataAdapter da = new SqlDataAdapter(SELECT * FROM Orders, ConfigurationSettings.AppSettings[Sql_ConnectString]); DataTable dtOrders = new DataTable(Orders); da.FillSchema(dtOrders, SchemaType.Source); da.Fill(dtOrders); // Create a view and bind it to the grid. dv = new DataView(dtOrders); dataGrid.DataSource = dv;}private void allowDeleteCheckBox_CheckedChanged(object sender, System.EventArgs e){ dv.AllowDelete = allowDeleteCheckBox.Checked;}private void allowEditCheckBox_CheckedChanged(object sender, System.EventArgs e){ dv.AllowEdit = allowEditCheckBox.Checked;}private void allowInsertCheckBox_CheckedChanged(object sender, System.EventArgs e){ dv.AllowNew = allowInsertCheckBox.Checked;}DiscussionThe DataGrid control does not have properties that control the adding, editing, or deletingthe data in the control. Binding a DataGrid to a DataTable binds to the default view of theunderlying DataTable.The DataView class represents a view of the DataTable that can be data bound on bothWindows Forms and Web Forms. The DataView can be customized for editing, filtering,searching, and sorting.The DataView class can be used to add, edit, or delete records in the underlyingDataTable. The properties described in Table 7-14 control the data modificationpermitted in a DataView. Table 7-14. DataView properties Property DescriptionAllowDelete Gets or sets a Boolean value indicating whether deletes are allowedAllowEdit Gets or sets a Boolean value indicating whether edits are allowedAllowNew Gets or sets a Boolean value indicating whether new rows can be addedIf AllowNew is true, the record is not added until the EndEdit( ) method is called eitherexplicitly or implicitly. The CancelEdit( ) method of the DataRowView can be called todiscard the row before it is added.If AllowEdit is true, the changes to the row are not committed until the EndEdit( )method is called either explicitly or implicitly. Only one row can be edited at a time. TheCancelEdit( ) method of the DataRowView can be called to discard the row before it isadded.If the AddNew( ) or BeginEdit( ) method of the DataRowView is called, EndEdit( ) iscalled implicitly on the pending row; this applies the changes to the row in the underlyingDataTable. In data controls that allow editing multiple records, EndEdit( ) is calledimplicitly when the current row is changed.[ Team LiB ]
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 Displaying an Image from a Database in a Web Forms ControlTà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