如何:将 Windows 窗体控件绑定到类型

更新:2007 年 11 月

生成与数据交互的控件时,您有时会发现需要将控件绑定到类型而不是对象。这种情况尤其会在设计时出现,那时数据可能不可用,但数据绑定的控件仍需要显示类型的公共接口的信息。例如,您可以将 DataGridView 控件绑定到由 Web 服务公开的对象,并希望在设计时 DataGridView 控件用自定义类型的成员名称来标记列。

可以使用 BindingSource 控件容易地将控件绑定到类型。

示例

下面的代码示例演示如何使用 BindingSource 控件将 DataGridView 控件绑定到自定义类型。在运行该示例时,您将注意到在用数据填充 DataGridView 控件之前,该控件中已经对那些反映 Customer 对象属性的列进行了标记。该示例有一个用来向 DataGridView 控件添加数据的“添加客户”按钮。当您单击该按钮时,会向 BindingSource 中添加一个新的 Customer 对象。在实际方案中,数据可以通过调用 Web 服务或其他数据源来获取。

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms

Class Form1
    Inherits Form
    Private bSource As New BindingSource()
    Private WithEvents button1 As Button
    Private dgv As New DataGridView()

    Public Sub New()
        Me.button1 = New System.Windows.Forms.Button()
        Me.button1.Location = New System.Drawing.Point(140, 326)
        Me.button1.Name = "button1"
        Me.button1.AutoSize = True
        Me.button1.Text = "Add Customer"
        Me.ClientSize = New System.Drawing.Size(362, 370)
        Me.Controls.Add(Me.button1)

        ' Bind the BindingSource to the DemoCustomer type.
        bSource.DataSource = GetType(DemoCustomer)

        ' Set up the DataGridView control.
        dgv.Dock = DockStyle.Top
        Me.Controls.Add(dgv)

        ' Bind the DataGridView control to the BindingSource.
        dgv.DataSource = bSource

    End Sub

    Public Shared Sub Main()
        Application.Run(New Form1())

    End Sub

    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles button1.Click
        bSource.Add(New DemoCustomer(DateTime.Today))

    End Sub
End Class

' This simple class is used to demonstrate binding to a type.
Public Class DemoCustomer

    Public Sub New()
        idValue = Guid.NewGuid()
    End Sub

    Public Sub New(ByVal FirstOrderDate As DateTime)
        FirstOrder = FirstOrderDate
        idValue = Guid.NewGuid()
    End Sub

    ' These fields hold the data that backs the public properties.
    Private firstOrderDateValue As DateTime
    Private idValue As Guid
    Private custNameValue As String

    Public Property CustomerName() As String
        Get
            Return custNameValue
        End Get
        Set(ByVal value As String)
            custNameValue = value
        End Set
    End Property

    ' This is a property that represents the first order date.
    Public Property FirstOrder() As DateTime
        Get
            Return Me.firstOrderDateValue
        End Get
        Set(ByVal value As DateTime)
            If value <> Me.firstOrderDateValue Then
                Me.firstOrderDateValue = value
            End If
        End Set
    End Property

    ' This is a property that represents a customer ID.
    Public ReadOnly Property ID() As Guid
        Get
            Return Me.idValue
        End Get
    End Property
End Class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

class Form1 : Form
{
    BindingSource bSource = new BindingSource();
    private Button button1;
    DataGridView dgv = new DataGridView();

    public Form1()
    {
        this.button1 = new System.Windows.Forms.Button();
        this.button1.Location = new System.Drawing.Point(140, 326);
        this.button1.Name = "button1";
        this.button1.AutoSize = true;
        this.button1.Text = "Add Customer";
        this.button1.Click += new System.EventHandler(this.button1_Click);
        this.ClientSize = new System.Drawing.Size(362, 370);
        this.Controls.Add(this.button1);

        // Bind the BindingSource to the DemoCustomer type.
        bSource.DataSource = typeof(DemoCustomer);

        // Set up the DataGridView control.
        dgv.Dock = DockStyle.Top;
        this.Controls.Add(dgv);

        // Bind the DataGridView control to the BindingSource.
        dgv.DataSource = bSource;

    }
    public static void Main()
    {
        Application.Run(new Form1());

    }

    private void button1_Click(object sender, EventArgs e)
    {
        bSource.Add(new DemoCustomer(DateTime.Today));
    }
}

// This simple class is used to demonstrate binding to a type.
public class DemoCustomer
{
    public DemoCustomer()
    {
        idValue = Guid.NewGuid();
    }

    public DemoCustomer(DateTime FirstOrderDate)
    {
        FirstOrder = FirstOrderDate;
        idValue = Guid.NewGuid();
    }
    // These fields hold the data that backs the public properties.
    private DateTime firstOrderDateValue;
    private Guid idValue;
    private string custNameValue;

    public string CustomerName
    {
        get { return custNameValue; }
        set { custNameValue = value; }
    }
    
    // This is a property that represents a birth date.
    public DateTime FirstOrder
    {
        get
        {
            return this.firstOrderDateValue;
        }
        set
        {
            if (value != this.firstOrderDateValue)
            {
                this.firstOrderDateValue = value;
            }
        }
    }

    // This is a property that represents a customer ID.
    public Guid ID
    {
        get
        {
            return this.idValue;
        }
    }
}

编译代码

此示例要求:

  • 对 System 和 System.Windows.Forms 程序集的引用。

有关从 Visual Basic 或 Visual C# 的命令行生成此示例的信息,请参见从命令行生成 (Visual Basic)在命令行上使用 csc.exe 生成。也可以通过将代码粘贴到新项目,在 Visual Studio 中生成此示例。如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例
如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例
如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例
如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例
如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例

请参见

参考

BindingNavigator

DataGridView

BindingSource

其他资源

BindingSource 组件