Filtering and Sorting Data
Số trang: 4
Loại file: pdf
Dung lượng: 15.27 KB
Lượt xem: 6
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 3.1 Filtering and Sorting Data Problem You have a DataSet filled with data, but you need to work with only a subset of the records and also to sort them. You need a way to both filter and sort the records in your DataSet without requerying the data source.
Nội dung trích xuất từ tài liệu:
Filtering and Sorting Data [ Team LiB ]Recipe 3.1 Filtering and Sorting DataProblemYou have a DataSet filled with data, but you need to work with only a subset of therecords and also to sort them. You need a way to both filter and sort the records in yourDataSet without requerying the data source.SolutionUse DataViewManager and DataView objects to filter and sort a DataSet.The sample code contains two event handlers:Form.Load Sets up the sample by creating a DataSet containing the Customers and Orders tables from the Northwind sample database and a relation between them. The default view for the Customers table is bound to the data grid on the form.Refresh Button.Click Applies the filters and sort order specified by the user to the data views for the tables accessed through the DataViewManager object.The C# code is shown in Example 3-1.Example 3-1. File: FilterSortForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Data;using System.Data.SqlClient;// Table name constantsprivate const String CUSTOMERS_TABLE = Customers;private const String ORDERS_TABLE = Orders;// Relation name constantsprivate const String CUSTOMERS_ORDERS_RELATION = Customers_Orders_Relation;// Field name constantsprivate const String CUSTOMERID_FIELD = CustomerID;private const String ORDERID_FIELD = OrderID;private const String CONTACTNAME_FIELD = ContactName;private DataSet ds;// . . .private void FilterSortForm_Load(object sender, System.EventArgs e){ ds = new DataSet( ); SqlDataAdapter da; // Fill the Customers table and add it to the DataSet. da = new SqlDataAdapter(SELECT * FROM Customers, ConfigurationSettings.AppSettings[Sql_ConnectString]); DataTable customersTable = new DataTable(CUSTOMERS_TABLE); da.Fill(customersTable); ds.Tables.Add(customersTable); // Fill the Order table and add it to the DataSet. da = new SqlDataAdapter(SELECT * FROM Orders, ConfigurationSettings.AppSettings[Sql_ConnectString]); DataTable orderTable = new DataTable(ORDERS_TABLE); da.Fill(orderTable); ds.Tables.Add(orderTable); // Create a relation between the tables. ds.Relations.Add(CUSTOMERS_ORDERS_RELATION, ds.Tables[CUSTOMERS_TABLE].Columns[CUSTOMERID_FIELD], ds.Tables[ORDERS_TABLE].Columns[CUSTOMERID_FIELD], true); // Bind the DataViewManager to the grid. dataGrid.SetDataBinding(ds.DefaultViewManager, CUSTOMERS_TABLE);}private void refreshButton_Click(object sender, System.EventArgs e){ DataViewManager dvm = new DataViewManager(ds); String countryFilter = ; if (customerCountryTextBox.Text != ) countryFilter = Country = + customerCountryTextBox.Text + ; // Sort on the contact name, as appropriate. if(contactSortCheckBox.Checked) dvm.DataViewSettings[CUSTOMERS_TABLE].Sort = CONTACTNAME_FIELD; // Filter the Customers view for the country. dvm.DataViewSettings[CUSTOMERS_TABLE].RowFilter = countryFilter; // Filter to Orders view for the employee. String employeeIdFilter = ; if (orderEmployeeIdTextBox.Text != ) { try { employeeIdFilter = EmployeeId = + Int32.Parse(orderEmployeeIdTextBox.Text); } catch (FormatException) { orderEmployeeIdTextBox.Text = ; } } dvm.DataViewSettings[ORDERS_TABLE].RowFilter = employeeIdFilter; // Bind the DataViewManager to the grid. dataGrid.SetDataBinding(dvm, CUSTOMERS_TABLE);}DiscussionThe DataView filters and sorts the data in DataTable objects in the DataSet. TheDataViewManager can simplify working with multiple views within a DataSet, but is notrequired. The DataViewManager object exposes a DataViewSettingCollection objectthrough the DataViewSettings property. The collection contains a singleDataViewSetting object for each table in the DataSet. The object is accessed using thename or ordinal of the table by using an indexer in C# or by using the Item( ) property inVB.NET. The DataViewSetting object allows access to the ApplyDefaultSort, RowFilter,RowStateFilter, and Sort properties of a DataView created from the DataViewManagerfor the table. Accessing these properties is identical to accessing the same propertiesdirectly through the DataView.The RowFilter property of the DataView accesses the expression that filters the view.The Sort property of the DataView sorts the view on a single or multiple columns ineither ascending or descending order.In the sample, a filter field is provided on both the Orders and Order Details table (theCountry and EmployeeID fields, respectively). Additionally, the sample allows the datagrid to be optionally sorted on the ContactName column. The filter and sort properties arecontrolled by setting the RowFilter and Sort properties of the DataViewSetting for theappropriate table.[ Team LiB ]
Nội dung trích xuất từ tài liệu:
Filtering and Sorting Data [ Team LiB ]Recipe 3.1 Filtering and Sorting DataProblemYou have a DataSet filled with data, but you need to work with only a subset of therecords and also to sort them. You need a way to both filter and sort the records in yourDataSet without requerying the data source.SolutionUse DataViewManager and DataView objects to filter and sort a DataSet.The sample code contains two event handlers:Form.Load Sets up the sample by creating a DataSet containing the Customers and Orders tables from the Northwind sample database and a relation between them. The default view for the Customers table is bound to the data grid on the form.Refresh Button.Click Applies the filters and sort order specified by the user to the data views for the tables accessed through the DataViewManager object.The C# code is shown in Example 3-1.Example 3-1. File: FilterSortForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Data;using System.Data.SqlClient;// Table name constantsprivate const String CUSTOMERS_TABLE = Customers;private const String ORDERS_TABLE = Orders;// Relation name constantsprivate const String CUSTOMERS_ORDERS_RELATION = Customers_Orders_Relation;// Field name constantsprivate const String CUSTOMERID_FIELD = CustomerID;private const String ORDERID_FIELD = OrderID;private const String CONTACTNAME_FIELD = ContactName;private DataSet ds;// . . .private void FilterSortForm_Load(object sender, System.EventArgs e){ ds = new DataSet( ); SqlDataAdapter da; // Fill the Customers table and add it to the DataSet. da = new SqlDataAdapter(SELECT * FROM Customers, ConfigurationSettings.AppSettings[Sql_ConnectString]); DataTable customersTable = new DataTable(CUSTOMERS_TABLE); da.Fill(customersTable); ds.Tables.Add(customersTable); // Fill the Order table and add it to the DataSet. da = new SqlDataAdapter(SELECT * FROM Orders, ConfigurationSettings.AppSettings[Sql_ConnectString]); DataTable orderTable = new DataTable(ORDERS_TABLE); da.Fill(orderTable); ds.Tables.Add(orderTable); // Create a relation between the tables. ds.Relations.Add(CUSTOMERS_ORDERS_RELATION, ds.Tables[CUSTOMERS_TABLE].Columns[CUSTOMERID_FIELD], ds.Tables[ORDERS_TABLE].Columns[CUSTOMERID_FIELD], true); // Bind the DataViewManager to the grid. dataGrid.SetDataBinding(ds.DefaultViewManager, CUSTOMERS_TABLE);}private void refreshButton_Click(object sender, System.EventArgs e){ DataViewManager dvm = new DataViewManager(ds); String countryFilter = ; if (customerCountryTextBox.Text != ) countryFilter = Country = + customerCountryTextBox.Text + ; // Sort on the contact name, as appropriate. if(contactSortCheckBox.Checked) dvm.DataViewSettings[CUSTOMERS_TABLE].Sort = CONTACTNAME_FIELD; // Filter the Customers view for the country. dvm.DataViewSettings[CUSTOMERS_TABLE].RowFilter = countryFilter; // Filter to Orders view for the employee. String employeeIdFilter = ; if (orderEmployeeIdTextBox.Text != ) { try { employeeIdFilter = EmployeeId = + Int32.Parse(orderEmployeeIdTextBox.Text); } catch (FormatException) { orderEmployeeIdTextBox.Text = ; } } dvm.DataViewSettings[ORDERS_TABLE].RowFilter = employeeIdFilter; // Bind the DataViewManager to the grid. dataGrid.SetDataBinding(dvm, CUSTOMERS_TABLE);}DiscussionThe DataView filters and sorts the data in DataTable objects in the DataSet. TheDataViewManager can simplify working with multiple views within a DataSet, but is notrequired. The DataViewManager object exposes a DataViewSettingCollection objectthrough the DataViewSettings property. The collection contains a singleDataViewSetting object for each table in the DataSet. The object is accessed using thename or ordinal of the table by using an indexer in C# or by using the Item( ) property inVB.NET. The DataViewSetting object allows access to the ApplyDefaultSort, RowFilter,RowStateFilter, and Sort properties of a DataView created from the DataViewManagerfor the table. Accessing these properties is identical to accessing the same propertiesdirectly through the DataView.The RowFilter property of the DataView accesses the expression that filters the view.The Sort property of the DataView sorts the view on a single or multiple columns ineither ascending or descending order.In the sample, a filter field is provided on both the Orders and Order Details table (theCountry and EmployeeID fields, respectively). Additionally, the sample allows the datagrid to be optionally sorted on the ContactName column. The filter and sort properties arecontrolled by setting the RowFilter and Sort properties of the DataViewSetting for theappropriate table.[ 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 Filtering and Sorting DataGợi ý tài liệu liên quan:
-
52 trang 430 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 314 0 0 -
74 trang 299 0 0
-
96 trang 293 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 289 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 281 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 275 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 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 265 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 265 0 0