zoukankan      html  css  js  c++  java
  • 如何:使用 Windows 窗体 BindingSource 组件对 ADO.NET 数据进行排序和筛选

    You can expose the sorting and filtering capability of BindingSource control through the Sort and Filter properties. You can apply simple sorting when the underlying data source is an IBindingList, and you can apply filtering and advanced sorting when the data source is an IBindingListView. The Sort property requires standard ADO.NET syntax: a string representing the name of a column of data in the data source followed by ASC or DESC to indicate whether the list should be sorted in ascending or descending order. You can set advanced sorting or multiple-column sorting by separating each column with a comma separator. The Filter property takes a string expression.

     Note

    Storing sensitive information, such as a password, within the connection string can affect the security of your application. Using Windows Authentication (also known as integrated security) is a more secure way to control access to a database. For more information, see Protecting Connection Information.

    To filter data with the BindingSource

    • Set the Filter property to expression that you want.

      In the following code example, the expression is a column name followed by value that you want for the column.

    BindingSource1.Sort = "Country DESC, Address ASC";

    Example

    The following code example loads data from the Customers table of the Northwind sample database into a DataGridView control, and filters and sorts the displayed data.

    private void InitializeSortedFilteredBindingSource()
    {
        // Create the connection string, data adapter and data table.
        SqlConnection connectionString =
             new SqlConnection("Initial Catalog=Northwind;" +
             "Data Source=localhost;Integrated Security=SSPI;");
        SqlDataAdapter customersTableAdapter =
            new SqlDataAdapter("Select * from Customers", connectionString);
        DataTable customerTable = new DataTable();
    
        // Fill the adapter with the contents of the customer table.
        customersTableAdapter.Fill(customerTable);
    
        // Set data source for BindingSource1.
        BindingSource1.DataSource = customerTable;
    
        // Filter the items to show contacts who are owners.
        BindingSource1.Filter = "ContactTitle='Owner'";
    
        // Sort the items on the company name in descending order.
        BindingSource1.Sort = "Country DESC, Address ASC";
    
        // Set the data source for dataGridView1 to BindingSource1.
        dataGridView1.DataSource = BindingSource1;
    }

    Compiling the Code

    To run this example, paste the code into a form that contains a BindingSource named BindingSource1 and a DataGridView named dataGridView1. Handle the Load event for the form and call InitializeSortedFilteredBindingSource in the load event handler method.

  • 相关阅读:
    Qt线程的简单使用(四)——QSemaphore的使用
    Qt线程的简单使用(三)——通过一个实例理解QMutex的使用
    Qt线程的简单使用(二)——通过继承QThread()创建线程
    Qt线程的简单使用(一)——通过QObject::moveToThread()创建线程
    高斯投影与UTM的异同
    基于Qt的Tcp协议的流程图
    OC系列高级-block
    OC系列foundation Kit基础-NSDate
    OC系列高级-内存管理关键字
    OC系列高级-NSValue
  • 原文地址:https://www.cnblogs.com/wfy680/p/14798028.html
Copyright © 2011-2022 走看看