• 如何:使用 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.

  • 相关阅读:
    自适应PC端网页制作使用REM
    iis express添加虚拟目录
    VC++2010组件安装失败解决办法
    AXURE 8弄一个轮播图的步骤
    浏览器在DPI缩放时变化问题
    数据结构综合训练1
    数据结构上机5二叉树遍历
    数据结构-约瑟夫环
    数据结构上机4队列-杨辉三角2
    数据结构上机4队列-杨辉三角1
  • 原文地址:https://www.cnblogs.com/wfy680/p/14798028.html
Copyright © 2020-2023  润新知