مشاركة عبر


كيفية القيام بما يلي: الوصول إلى الكائنات المنضمه إلى صفوف DataGridView الخاصة بنماذج Windows

في بعض الأحيان من المفيد عرض جدول معلومات مخزنة في مجموعة من كائنات أعمال. عندما يربط عنصر تحكم DataGridView لمثل هذه المجموعة, كل خاصية عامة يتم عرضها في العمود الخاص به إلا إذا تم تعليم الخاصية غير قابلة للاستعراض مع BrowsableAttribute. على سبيل المثال، مجموعة من كائنات Customer سيكون لها أعمدة مثل اسم و عنوان.

إذا كانت هذه الكائنات تحتوي على معلومات إضافية و تعليمات برمجية تريد الوصول إليها, يمكن أن تصل إليها عبر كائنات الصف. في مثال التعليمات البرمجية التالي, يمكن للمستخدمين تحديد صفوف متعددة ثم النقر فوق زر يرسل فاتورة إلى كل من العملاء المطابقين.

للوصول إلى كائنات مرتبطة بصف

  • استخدم الخاصية DataGridViewRow.DataBoundItem.

    Private Sub InvoiceButton_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles InvoiceButton.Click
    
        For Each row As DataGridViewRow In Me.DataGridView1.SelectedRows
    
            Dim cust As Customer = TryCast(row.DataBoundItem, Customer)
            If cust IsNot Nothing Then
                cust.SendInvoice()
            End If
    
        Next
    
    End Sub
    
    void invoiceButton_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in this.dataGridView1.SelectedRows)
        {
            Customer cust = row.DataBoundItem as Customer;
            if (cust != null)
            {
                cust.SendInvoice();
            }
        }
    }
    

مثال

يتضمن المثال التعليمات البرمجية الكامل تطبيق Customer بسيط ويربط DataGridView إلى ArrayList يحتوي على عدد قليل من كائنات Customer. معالج الأحداث Click الخاص بSystem.Windows.Forms.Button يجب أن يصل إلى كائنات Customer عبر الصفوف، لأن مجموعة العميل لا يمكن الوصول إليها من خارج معالج الأحداث Form.Load.

Imports System
Imports System.Windows.Forms

Public Class DataGridViewObjectBinding
    Inherits Form

    ' These declarations and the Main() and New() methods 
    ' below can be replaced with designer-generated code. 
    Private WithEvents InvoiceButton As New Button()
    Private WithEvents DataGridView1 As New DataGridView()

    ' Entry point code. 
    <STAThreadAttribute()> _
    Public Shared Sub Main()

        Application.Run(New DataGridViewObjectBinding())

    End Sub

    ' Sets up the form. 
    Public Sub New()

        Me.DataGridView1.Dock = DockStyle.Fill
        Me.Controls.Add(Me.DataGridView1)

        Me.InvoiceButton.Text = "invoice the selected customers"
        Me.InvoiceButton.Dock = DockStyle.Top
        Me.Controls.Add(Me.InvoiceButton)
        Me.Text = "DataGridView collection-binding demo"

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load

        ' Set up a collection of objects for binding.
        Dim customers As New System.Collections.ArrayList()
        customers.Add(New Customer("Harry"))
        customers.Add(New Customer("Sally"))
        customers.Add(New Customer("Roy"))
        customers.Add(New Customer("Pris"))

        ' Initialize and bind the DataGridView.
        Me.DataGridView1.SelectionMode = _
            DataGridViewSelectionMode.FullRowSelect
        Me.DataGridView1.AutoGenerateColumns = True
        Me.DataGridView1.DataSource = customers

    End Sub

    ' Calls the SendInvoice() method for the Customer 
    ' object bound to each selected row.
    Private Sub InvoiceButton_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles InvoiceButton.Click

        For Each row As DataGridViewRow In Me.DataGridView1.SelectedRows

            Dim cust As Customer = TryCast(row.DataBoundItem, Customer)
            If cust IsNot Nothing Then
                cust.SendInvoice()
            End If

        Next

    End Sub

End Class

Public Class Customer

    Private nameValue As String

    Public Sub New(ByVal name As String)
        nameValue = name
    End Sub

    Public Property Name() As String
        Get
            Return nameValue
        End Get
        Set(ByVal value As String)
            nameValue = value
        End Set
    End Property

    Public Sub SendInvoice()
        MsgBox(nameValue & " has been billed.")
    End Sub

End Class
using System;
using System.Windows.Forms;

public class DataGridViewObjectBinding : Form
{
    // These declarations and the Main() and New() methods 
    // below can be replaced with designer-generated code. 
    private Button invoiceButton = new Button();
    private DataGridView dataGridView1 = new DataGridView();

    // Entry point code. 
    [STAThreadAttribute()]
    public static void Main()
    {
        Application.Run(new DataGridViewObjectBinding());
    }

    // Sets up the form.
    public DataGridViewObjectBinding()
    {
        this.dataGridView1.Dock = DockStyle.Fill;
        this.Controls.Add(this.dataGridView1);

        this.invoiceButton.Text = "invoice the selected customers";
        this.invoiceButton.Dock = DockStyle.Top;
        this.invoiceButton.Click += new EventHandler(invoiceButton_Click);
        this.Controls.Add(this.invoiceButton);

        this.Load += new EventHandler(DataGridViewObjectBinding_Load);
        this.Text = "DataGridView collection-binding demo";
    }

    void  DataGridViewObjectBinding_Load(object sender, EventArgs e)
    {
        // Set up a collection of objects for binding.
        System.Collections.ArrayList customers = new System.Collections.ArrayList();
        customers.Add(new Customer("Harry"));
        customers.Add(new Customer("Sally"));
        customers.Add(new Customer("Roy"));
        customers.Add(new Customer("Pris"));

        // Initialize and bind the DataGridView.
        this.dataGridView1.SelectionMode = 
            DataGridViewSelectionMode.FullRowSelect;
        this.dataGridView1.AutoGenerateColumns = true;
        this.dataGridView1.DataSource = customers;
    }

    // Calls the SendInvoice() method for the Customer 
    // object bound to each selected row.
    void invoiceButton_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in this.dataGridView1.SelectedRows)
        {
            Customer cust = row.DataBoundItem as Customer;
            if (cust != null)
            {
                cust.SendInvoice();
            }
        }
    }
}

public class Customer
{
    private String nameValue;

    public Customer(String name)
    {
        nameValue = name;
    }

    public String Name
    {
        get
        {
            return nameValue;
        }
        set 
        {
            nameValue = value;
        }
    }

    public void SendInvoice()
    {
        MessageBox.Show(nameValue + " has been billed.");
    }
}

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

يتطلب هذا المثال:

  • مراجع عن تجميعات النظام و تجميعات System.Windows.Forms.

للحصول على المزيد من المعلومات حول إنشاء هذا المثال من خط أوامر Visual Basic أو #Visual C، راجع الإنشاء من سطر الأوامر (Visual Basic) أو إنشاء سطر الأوامر باستخدام csc.exe. يمكنك أيضاً بناء هذا المثال في Visual Studio عن طريق لصق التعليمات البرمجية في مشروع جديد. لمزيد من المعلومات، راجع: كيفية القيام بما يلي: ترجمة و تشغيل مثال التعليمات برمجية لنماذج Windows الكامل باستخدام ‏‫Visual Studio.

راجع أيضًا:

المهام

كيفية القيام بما يلي: يربط الكائنات إلى عناصر تحكم عرض شبكة البيانات Windows Forms

المرجع

DataGridView

DataGridViewRow

DataGridViewRow.DataBoundItem

موارد أخرى

عرض البيانات في عنصر التحكم DataGridView Windows Forms