مشاركة عبر


كيفية القيام بما يلي: فرز و تصفية بيانات ADO.NET باستخدام مكوّن BindingSource الخاص بنماذج Windows

يمكنك تعريض قدرة الفرز و التصفية الخاصه بعنصر تحكم BindingSource من خلال خصائص Sort و Filter. يمكنك تطبيق الفرز البسيطة عندما يكون مصدر البيانات الأساسي IBindingList، و يمكنك تطبيق التصفية و الفرز المتقدم عندما يكون مصدر البيانات IBindingListView. تتطلب خاصية Sort القياسية بناء جملة ADO.NET: سلسلة الأحرف التي تمثل اسم عمود البيانات في مصدر البيانات متبوعاً بـ ASC أو DESC للإشارة إلى ما إذا كان يجب أن يتم فرز القائمة في ترتيب تصاعدي أو تنازلي. يمكنك تعيين فرز متقدم أو فرز متعدد الأعمدة بواسطة فصل كل عمود مع فاصل الفاصلة. تأخذ خاصية Filter تعبير السلسلة.

ملاحظة

تخزين معلومات حساسة مثل كلمة مرور في سلسلة اتصال يمكن أن يؤثر على أمن التطبيق الخاص بك. استخدام طريقة مصادقة Windows (المعروف أيضا باسم الأمان المتكامل) تعتبر أكثر أماناً للتحكم في الوصول إلى قاعدة بيانات. لمزيد من المعلومات، راجع Protecting Connection Information (ADO.NET).

لتصفية بيانات باستخدام BindingSource

  • عين خاصية Filter للتعبير الذي تريده.

    في مثال التعليمات البرمجية التالي, التعبير هو اسم العمود متبوعاً بالقيمة التي تريدها له.

BindingSource1.Filter = "ContactTitle='Owner'"
            BindingSource1.Filter = "ContactTitle='Owner'";

لفرز البيانات باستخدام BindingSource

  1. قم بتعيين خاصية Sort إلى اسم العمود الذي تريده متبوعاً بـ ASC أو DESC لتوضيح الترتيب التصاعدي أو التنازلي.

  2. فصل الأعمدة المتعددة بفاصلة.

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

مثال

يقوم مثال التعليمات البرمجية التالي بتحميل البيانات من جدول "العملاء" من قاعدة بيانات نموذج Northwind إلى عنصر تحكم DataGridView، ثم يقوم بتصفية و فرز البيانات المعروضة.

Private Sub InitializeSortedFilteredBindingSource()

    ' Create the connection string, data adapter and data table.
    Dim connectionString As New SqlConnection("Initial Catalog=Northwind;" & _
        "Data Source=localhost;Integrated Security=SSPI;")
    Dim customersTableAdapter As New SqlDataAdapter("Select * from Customers", _
        connectionString)
    Dim customerTable As New DataTable()

    ' Fill the 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


End Sub

        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 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;

        }

التحويل البرمجي للتعليمات البرمجية

لتشغيل هذا المثال، قم بلصق التعليمات البرمجية في نموذج يحتوي على BindingSource يسمى BindingSource1 و DataGridView يسمى dataGridView1. معالجة حدث Load للنموذج و إستدعاء InitializeSortedFilteredBindingSource في أسلوب معالج حدث التحميل.

راجع أيضًا:

المهام

كيفية القيام بما يلي: يثّبت نموذج قواعد بيانات

المرجع

Sort

Filter

موارد أخرى

مكوّن BindingSource