Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
[ Team LiB ] Recipe 7.15 Adding Search Capabilities to Windows Forms Problem You need to use a search criteria specified by a user to locate a record displayed in a DataGrid without executing a query against the database. Solution Use the Find( ) | Team LiB Recipe 7.15 Adding Search Capabilities to Windows Forms Problem You need to use a search criteria specified by a user to locate a record displayed in a DataGrid without executing a query against the database. Solution Use the Find method of the DataView with a sort key value to locate a record displayed in a DataGrid and reposition the row in the DataGrid. The sample code contains two event handlers Form.Load Sets up the sample by creating a DataTable and filling it with the Customers table from the Northwind sample database. A DataView is created based on the default view of the Customers DataTable its sort key is set to the CustomerlD column and it is bound to the data grid on the form. Finally a CurrencyManager is created from the DataView. Go Button.Click Uses the Find method of the DataView to locate a record with the CustomerlD specified by the user. If the CustomerlD is found the CurrencyManager created in the Form.Load event handler is used to select the matching record in the data grid. The C code is shown in Example 7-31. Example 7-31. File SearchDataGridForm.es Namespaces variables and constants using System using System.Configuration using System.Windows.Forms using System.Data using System.Data.SqlClient private DataView dv private CurrencyManager cm . . . private void SearchDataGridForm_Load object sender System.EventArgs e Create the DataAdapter and load the Customers data in a table. String sqlText SELECT FROM Customers SqlDataAdapter da new SqlDataAdapter sqlText ConfigurationSettings.AppSettings S ql_ConnectString DataTable dt new DataTable da.Fill dt Create a view from the default view for the table. dv dt.DefaultView dv.Sort CustomerlD Bind the view to the grid. findDataGrid.DataSource dv Get the CurrencyManager for the DataView. cm CurrencyManager findDataGrid.BindingContext dv private void findButton_Click object sender System.EventArgs e if findTextBox.Text Find the customer. int i dv.Find findTextBox.Text if i 0 A match was not .